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

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.1       paf         5: 
1.16      paf         6:        Author: Alexander Petrosyan <paf@design.ru>(http://design.ru/paf)
1.1       paf         7: 
1.33    ! parser      8:        $Id: image.C,v 1.32 2001/08/27 15:47:13 parser Exp $
1.31      parser      9: */
1.33    ! parser     10: static const char *RCSId="$Id: image.C,v 1.32 2001/08/27 15:47:13 parser Exp $"; 
1.31      parser     11: 
                     12: /*
                     13:        jpegsize: gets the width and height (in pixels) of a jpeg file
                     14:        Andrew Tong, werdna@ugcs.caltech.edu           February 14, 1995
                     15:        modified slightly by alex@ed.ac.uk
                     16:        and further still by rjray@uswest.com
                     17:        optimization and general re-write from tmetro@vl.com
                     18:        from perl by paf@design.ru
1.1       paf        19: */
                     20: 
                     21: #include "pa_config_includes.h"
                     22: 
1.8       paf        23: #include "gif.h"
1.6       paf        24: 
1.1       paf        25: #include "pa_common.h"
                     26: #include "pa_request.h"
                     27: #include "pa_vfile.h"
                     28: #include "pa_vimage.h"
                     29: 
1.22      paf        30: // defines
1.1       paf        31: 
1.22      paf        32: #define IMAGE_CLASS_NAME "image"
                     33: 
                     34: // class
                     35: 
                     36: class MImage : public Methoded {
                     37: public: // VStateless_class
                     38:        Value *create_new_value(Pool& pool) { return new(pool) VImage(pool); }
                     39: 
                     40: public:
                     41:        MImage(Pool& pool);
1.24      paf        42: 
                     43: public: // Methoded
1.22      paf        44:        bool used_directly() { return true; }
                     45: 
                     46: };
1.1       paf        47: 
                     48: // helpers
                     49: 
1.21      paf        50: /// simple buffered reader[from memory/file], used in _measure
1.1       paf        51: class Measure_reader {
                     52: public:
1.31      parser     53:        enum { READ_CHUNK_SIZE=0x400*10 };// 10K
1.16      paf        54:        typedef size_t(*Func)(void *& buf, size_t limit, void *info);
1.1       paf        55: 
                     56:        Measure_reader(Func afunc, void *ainfo) : 
                     57:                func(afunc), info(ainfo), 
                     58:                chunk(0), offset(0), size(0) {
                     59:        }
                     60: 
1.31      parser     61:        size_t read(void *&buf, size_t limit) {
1.3       paf        62:                if(offset+limit>size) // nothing left
                     63:                        if(offset==0 || limit==1) { // only one-byte continuations allowed
                     64:                                size=(*func)(chunk, READ_CHUNK_SIZE, info);
                     65:                                offset=0;
                     66:                        } else
1.16      paf        67:                                return 0;// as if EOF
1.1       paf        68:                if(!size) // EOF
                     69:                        return 0;
                     70:                        
                     71:                // something left
                     72:                size_t read_size=min(offset+limit, size)-offset;
                     73:                buf=((unsigned char *)chunk)+offset;
                     74:                offset+=read_size;
                     75:                return read_size;
                     76:        }
                     77: 
                     78: private:
                     79:        Func func;
                     80:        void *info;
                     81: 
                     82:        void *chunk;
                     83:        size_t offset;
                     84:        size_t size;
                     85: };
                     86: 
1.21      paf        87: /// GIF file header
1.1       paf        88: struct GIF_Header {
1.16      paf        89:        char       type[3];         // 'GIF'
1.1       paf        90:        char       version[3];
                     91:        unsigned char       width[2];
                     92:        unsigned char       height[2];
                     93:        char       dif;
                     94:        char       fonColor;
                     95:        char       nulls;
                     96: };
                     97: 
1.31      parser     98: /// JPEG record head
                     99: struct JPG_Segment_head {
                    100:        unsigned char marker;
                    101:        unsigned char code;
                    102:        unsigned char length[2];
1.1       paf       103: };
1.21      paf       104: /// JPEG frame header
1.31      parser    105: struct JPG_Size_segment_body {
1.27      parser    106:        char data;                    //< data precision of bits/sample
1.31      parser    107:        unsigned char height[2];               //< image height
                    108:        unsigned char width[2];                //< image width
1.27      parser    109:        char numComponents;           //< number of color components
1.1       paf       110: };
                    111: 
                    112: //
                    113: 
1.33    ! parser    114: inline short x_endian_to_int(unsigned char L, unsigned char H) {
        !           115:        return(short)((H<<8) + L);
        !           116: }
        !           117: 
        !           118: inline short big_endian_to_int(unsigned char b[2]) {
        !           119:        return x_endian_to_int(b[1], b[0]);
        !           120: }
        !           121: 
        !           122: inline short little_endian_to_int(unsigned char b[2]) {
        !           123:        return x_endian_to_int(b[0], b[1]);
1.1       paf       124: }
                    125: 
                    126: void measure_gif(Pool& pool, const String *origin_string, 
                    127:                         Measure_reader& reader, int& width, int& height) {
                    128: 
1.31      parser    129:        void *buf;
1.1       paf       130:        const int head_size=sizeof(GIF_Header);
                    131:        if(reader.read(buf, head_size)<head_size)
                    132:                PTHROW(0, 0, 
                    133:                        origin_string, 
                    134:                        "broken GIF header - file size is less then %d bytes", head_size);
1.31      parser    135:        GIF_Header *head=(GIF_Header *)buf;
1.1       paf       136: 
1.31      parser    137:        if(strncmp(head->type, "GIF", 3)!=0)
1.1       paf       138:                PTHROW(0, 0, 
                    139:                        origin_string, 
                    140:                        "bad image file - GIF signature not found");    
                    141: 
1.33    ! parser    142:        width=little_endian_to_int(head->width);
        !           143:        height=little_endian_to_int(head->height);
1.1       paf       144: }
                    145: 
                    146: void measure_jpeg(Pool& pool, const String *origin_string, 
                    147:                         Measure_reader& reader, int& width, int& height) {
1.2       paf       148:        // JFIF format markers
1.31      parser    149:        const unsigned char MARKER=0xFF;
                    150:        const unsigned char SIZE_FIRST=0xC0;
                    151:        const unsigned char SIZE_LAST=0xC3;
1.2       paf       152: 
1.31      parser    153:        void *buf;
1.18      paf       154:        const size_t prefix_size=2;
1.31      parser    155:        if(reader.read(buf, prefix_size)<prefix_size)
1.1       paf       156:                PTHROW(0, 0, 
                    157:                        origin_string, 
                    158:                        "broken JPEG file - size is less then %d bytes", prefix_size);
1.31      parser    159:        unsigned char *signature=(unsigned char *)buf;
1.1       paf       160:        
1.31      parser    161:        if(!(signature[0]==0xFF && signature[1]==0xD8)) 
1.1       paf       162:                PTHROW(0, 0, 
                    163:                        origin_string, 
1.31      parser    164:                        "broken JPEG file - signature not found");
                    165: 
                    166:        bool found=false;
                    167:        while(true) {
                    168:                void *buf;
                    169:         // Extract the segment header.
                    170:                if(reader.read(buf, sizeof(JPG_Segment_head))<sizeof(JPG_Segment_head))
                    171:                        break;          
                    172:                JPG_Segment_head *head=(JPG_Segment_head *)buf;
                    173: 
                    174:         // Verify that it's a valid segment.
                    175:                if(head->marker!=MARKER)
1.1       paf       176:                        break;
1.31      parser    177: 
                    178:                if(head->code >= SIZE_FIRST && head->code  <= SIZE_LAST) {
                    179:             // Segments that contain size info
                    180:                        if(reader.read(buf, sizeof(JPG_Size_segment_body))<sizeof(JPG_Size_segment_body))
                    181:                                break;
                    182:                        JPG_Size_segment_body *body=(JPG_Size_segment_body *)buf;
                    183:                        
1.32      parser    184:                        width=big_endian_to_int(body->width);
                    185:                        height=big_endian_to_int(body->height);
1.31      parser    186:                        found=true;
1.1       paf       187:                        break;
1.31      parser    188:                } else {
                    189:             // Dummy read to skip over data
1.32      parser    190:             size_t limit=big_endian_to_int(head->length) - 2;
1.31      parser    191:                        if(reader.read(buf, limit)<limit)
                    192:                                break;
                    193:         }
                    194:        }
                    195: 
                    196:        if(!found)
1.16      paf       197:                PTHROW(0, 0, 
                    198:                        origin_string, 
1.31      parser    199:                        "broken JPEG file - size frame not found");
1.1       paf       200: }
                    201: 
                    202: // measure center
                    203: 
                    204: void measure(Pool& pool, const String& file_name, 
                    205:                         Measure_reader& reader, int& width, int& height) {
                    206:        if(const char *cext=strrchr(file_name.cstr(), '.')) {
                    207:                cext++;
                    208:                if(strcasecmp(cext, "GIF")==0)
                    209:                        measure_gif(pool, &file_name, reader, width, height);
                    210:                else if(strcasecmp(cext, "JPG")==0 || strcasecmp(cext, "JPEG")==0) 
                    211:                        measure_jpeg(pool, &file_name, reader, width, height);
                    212:                else
                    213:                        PTHROW(0, 0, 
                    214:                                &file_name, 
                    215:                                "unhandled image file name extension '%s'", cext);
                    216:        } else
                    217:                PTHROW(0, 0, 
                    218:                        &file_name, 
                    219:                        "can not determine image type - no file name extension");
                    220: }
                    221: 
1.21      paf       222: /// used by image: _measure / read_mem
1.1       paf       223: struct Read_mem_info {
                    224:        unsigned char *ptr;
                    225:        unsigned char *eof;
                    226: };
                    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.21      paf       235: /// used by image: _measure / read_disk
1.1       paf       236: struct Read_disk_info {
                    237:        const String *file_spec;
                    238:        size_t offset;
                    239: };
                    240: static size_t read_disk(void*& buf, size_t limit, void *info) {
                    241:        Read_disk_info& rdi=*static_cast<Read_disk_info *>(info);
                    242:        Pool& pool=rdi.file_spec->pool();
                    243: 
                    244:        size_t read_size;
                    245:        file_read(pool, *rdi.file_spec, 
                    246:                           buf, read_size, 
                    247:                           false/*as_text*/, 
                    248:                           true/*fail_on_read_problem*/, 
                    249:                           rdi.offset, limit);
                    250: 
                    251:        rdi.offset+=read_size;
                    252:        return read_size;
                    253: }
                    254: 
                    255: // methods
                    256: 
1.17      paf       257: static void _measure(Request& r, const String& method_name, MethodParams *params) {
1.1       paf       258:        Pool& pool=r.pool();
                    259: 
1.30      parser    260:        Value& data=params->as_no_junction(0, "data must not be code");
1.1       paf       261: 
1.16      paf       262:        void *info;Measure_reader::Func read_func;
1.1       paf       263:        Read_mem_info read_mem_info;
                    264:        Read_disk_info read_disk_info;
                    265:        const String *file_name;
                    266:        if(data.is_string()) {
                    267:                file_name=data.get_string();
                    268:                read_disk_info.file_spec=&r.absolute(*file_name);
                    269:                read_disk_info.offset=0;
1.16      paf       270:                info=&read_disk_info;read_func=read_disk;
1.1       paf       271:        } else {
                    272:                const VFile& vfile=*data.as_vfile();
                    273:                file_name=&static_cast<Value *>(vfile.fields().get(*name_name))->as_string();
                    274:                read_mem_info.ptr=(unsigned char *)vfile.value_ptr();
                    275:                read_mem_info.eof=read_mem_info.ptr+vfile.value_size();
1.16      paf       276:                info=&read_mem_info;read_func=read_mem;
1.1       paf       277:        }
                    278: 
                    279:        Measure_reader reader(read_func, info);
                    280:        int width, height;
                    281:        measure(pool, *file_name, reader, width, height);
                    282: 
1.6       paf       283:        static_cast<VImage *>(r.self)->set(file_name, width, height);
1.1       paf       284: }
                    285: 
1.21      paf       286: /// used by image: _html / append_attrib_pair
1.4       paf       287: struct Attrib_info {
1.21      paf       288:        String *tag; ///< html tag being constructed
                    289:        Hash *skip; ///< tag attributes not to append to tag string [to skip]
1.4       paf       290: };
1.3       paf       291: static void append_attrib_pair(const Hash::Key& key, Hash::Val *val, void *info) {
1.4       paf       292:        Attrib_info& ai=*static_cast<Attrib_info *>(info);
                    293: 
                    294:        if(ai.skip && ai.skip->get(key))
                    295:                return;
                    296: 
1.3       paf       297:        Value& value=*static_cast<Value *>(val);
                    298:        // src="a.gif" width=123 ismap[=-1]
1.4       paf       299:        *ai.tag << " " << key;
1.26      parser    300:        if(value.is_string() || value.as_int()>=0)
1.6       paf       301:                *ai.tag << "=\"" << value.as_string() << "\"";
1.3       paf       302: }
1.17      paf       303: static void _html(Request& r, const String& method_name, MethodParams *params) {
1.3       paf       304:        Pool& pool=r.pool();
                    305: 
                    306:        String tag(pool);
                    307:        tag << "<img";
1.4       paf       308: 
1.18      paf       309:        const Hash& fields=static_cast<VImage *>(r.self)->fields();
1.5       paf       310:        Hash *attribs=0;
1.4       paf       311: 
1.11      paf       312:        if(params->size())
1.17      paf       313:                if(attribs=params->get(0).get_hash()) {
1.4       paf       314:                        Attrib_info attrib_info={&tag, 0};
1.5       paf       315:                        attribs->for_each(append_attrib_pair, &attrib_info);
1.4       paf       316:                } else
1.16      paf       317:                        PTHROW(0, 0, 
                    318:                                &method_name, 
1.5       paf       319:                                "attributes must be must be hash");
1.4       paf       320: 
1.5       paf       321:        Attrib_info attrib_info={&tag, attribs};
1.4       paf       322:        fields.for_each(append_attrib_pair, &attrib_info);
1.6       paf       323:        tag << " />";
1.3       paf       324:        r.write_pass_lang(tag);
                    325: }
1.8       paf       326: 
1.16      paf       327: static gdImage *load(Request& r, const String& method_name, 
                    328:                                         const String& file_name){
                    329:        Pool& pool=r.pool();
                    330: 
                    331:        const char *file_name_cstr=r.absolute(file_name).cstr(String::UL_FILE_NAME);
                    332:        if(FILE *f=fopen(file_name_cstr, "rb")) {
                    333:                gdImage& image=*new(pool) gdImage(pool);
1.23      paf       334:                bool ok=image.CreateFromGif(f);
1.16      paf       335:                fclose(f);
1.23      paf       336:                if(!ok)
                    337:                        PTHROW(0, 0, 
                    338:                                &file_name,
                    339:                                "is not in GIF format");
1.16      paf       340:                return &image;
                    341:        } else {
                    342:                PTHROW(0, 0, 
                    343:                        &method_name, 
                    344:                        "can not open '%s'", file_name_cstr);
                    345:                return 0;
                    346:        }
                    347: }
                    348: 
                    349: 
1.17      paf       350: static void _load(Request& r, const String& method_name, MethodParams *params) {
1.6       paf       351:        Pool& pool=r.pool();
                    352: 
1.30      parser    353:        Value& vfile_name=params->as_no_junction(0, "file name must not be code");
1.6       paf       354:        const String& file_name=vfile_name.as_string();
                    355: 
1.16      paf       356:        gdImage& image=*load(r, method_name, file_name);
                    357:        int width=image.SX();
                    358:        int height=image.SY();
                    359:        static_cast<VImage *>(r.self)->set(&file_name, width, height, &image);
1.6       paf       360: }
                    361: 
1.17      paf       362: static void _create(Request& r, const String& method_name, MethodParams *params) {
1.6       paf       363:        Pool& pool=r.pool();
                    364: 
1.26      parser    365:        int width=r.process(params->get(0)).as_int();
                    366:        int height=r.process(params->get(1)).as_int();
1.8       paf       367:        int bgcolor_value=0xffFFff;
1.6       paf       368:        if(params->size()>2)
1.26      parser    369:                bgcolor_value=r.process(params->get(2)).as_int();
1.15      paf       370:        gdImage& image=*new(pool) gdImage(pool);
                    371:        image.Create(width, height);
                    372:        image.FilledRectangle(0, 0, width-1, height-1, image.Color(bgcolor_value));
                    373:        static_cast<VImage *>(r.self)->set(0, width, height, &image);
1.6       paf       374: }
                    375: 
1.17      paf       376: static void _gif(Request& r, const String& method_name, MethodParams *params) {
1.6       paf       377:        Pool& pool=r.pool();
                    378: 
1.9       paf       379:        gdImage *image=static_cast<VImage *>(r.self)->image;
1.8       paf       380:        if(!image)
1.16      paf       381:                PTHROW(0, 0, 
                    382:                        &method_name, 
1.12      paf       383:                        "does not contain an image");
1.6       paf       384: 
                    385:        // could _ but don't thing it's wise to use $image.src for vfile.name
1.10      paf       386: 
1.16      paf       387:        String out(pool); image->Gif(out);
1.6       paf       388:        
                    389:        VFile& vfile=*new(pool) VFile(pool);
                    390:        String& image_gif=*new(pool) String(pool, "image/gif");
1.20      paf       391:        vfile.set(false/*not tainted*/, 
                    392:                out.cstr(String::UL_AS_IS), out.size(), 0, &image_gif);
1.6       paf       393: 
                    394:        r.write_no_lang(vfile);
                    395: }
                    396: 
1.17      paf       397: static void _line(Request& r, const String& method_name, MethodParams *params) {
1.12      paf       398:        Pool& pool=r.pool();
                    399: 
                    400:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    401:        if(!image)
1.16      paf       402:                PTHROW(0, 0, 
                    403:                        &method_name, 
1.12      paf       404:                        "does not contain an image");
                    405: 
                    406:        image->Line(
1.26      parser    407:                r.process(params->get(0)).as_int(), 
                    408:                r.process(params->get(1)).as_int(), 
                    409:                r.process(params->get(2)).as_int(), 
                    410:                r.process(params->get(3)).as_int(), 
                    411:                image->Color(r.process(params->get(4)).as_int()));
1.13      paf       412: }
                    413: 
1.17      paf       414: static void _fill(Request& r, const String& method_name, MethodParams *params) {
1.13      paf       415:        Pool& pool=r.pool();
                    416: 
                    417:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    418:        if(!image)
1.16      paf       419:                PTHROW(0, 0, 
                    420:                        &method_name, 
1.13      paf       421:                        "does not contain an image");
1.12      paf       422: 
1.13      paf       423:        image->Fill(
1.26      parser    424:                r.process(params->get(0)).as_int(), 
                    425:                r.process(params->get(1)).as_int(), 
                    426:                image->Color(r.process(params->get(2)).as_int()));
1.13      paf       427: }
                    428: 
1.17      paf       429: static void _rectangle(Request& r, const String& method_name, MethodParams *params) {
1.13      paf       430:        Pool& pool=r.pool();
                    431: 
                    432:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    433:        if(!image)
1.16      paf       434:                PTHROW(0, 0, 
                    435:                        &method_name, 
1.13      paf       436:                        "does not contain an image");
                    437: 
                    438:        image->Rectangle(
1.26      parser    439:                r.process(params->get(0)).as_int(), 
                    440:                r.process(params->get(1)).as_int(), 
                    441:                r.process(params->get(2)).as_int(), 
                    442:                r.process(params->get(3)).as_int(), 
                    443:                image->Color(r.process(params->get(4)).as_int()));
1.13      paf       444: }
                    445: 
1.17      paf       446: static void _bar(Request& r, const String& method_name, MethodParams *params) {
1.13      paf       447:        Pool& pool=r.pool();
                    448: 
                    449:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    450:        if(!image)
1.16      paf       451:                PTHROW(0, 0, 
                    452:                        &method_name, 
1.13      paf       453:                        "does not contain an image");
                    454: 
                    455:        image->FilledRectangle(
1.26      parser    456:                r.process(params->get(0)).as_int(), 
                    457:                r.process(params->get(1)).as_int(), 
                    458:                r.process(params->get(2)).as_int(), 
                    459:                r.process(params->get(3)).as_int(), 
                    460:                image->Color(r.process(params->get(4)).as_int()));
1.13      paf       461: }
                    462: 
1.17      paf       463: static void _replace(Request& r, const String& method_name, MethodParams *params) {
1.13      paf       464:        Pool& pool=r.pool();
                    465: 
                    466:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    467:        if(!image)
1.16      paf       468:                PTHROW(0, 0, 
                    469:                        &method_name, 
1.13      paf       470:                        "does not contain an image");
                    471: 
                    472:        if((params->size()-2)%2) // I see your thoughts, but that's more readable
1.16      paf       473:                PTHROW(0, 0, 
                    474:                        &method_name, 
1.13      paf       475:                        "y coordinate missing");
                    476: 
                    477:        int n=(params->size()-2)/2;
                    478:        
                    479:        gdImage::Point *p=(gdImage::Point *)pool.malloc(sizeof(gdImage::Point)*n);
                    480:        for(int i=0; i<n; i++) {
1.26      parser    481:                p[i].x=r.process(params->get(2+i*2+0)).as_int();
                    482:                p[i].y=r.process(params->get(2+i*2+1)).as_int();
1.13      paf       483:        }
1.16      paf       484:        image->FilledPolygonReplaceColor(p, n, 
1.26      parser    485:                image->Color(r.process(params->get(0)).as_int()), // src color
                    486:                image->Color(r.process(params->get(1)).as_int()));// dest color
1.13      paf       487: }
                    488: 
1.17      paf       489: static void _polygon(Request& r, const String& method_name, MethodParams *params) {
1.13      paf       490:        Pool& pool=r.pool();
                    491: 
                    492:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    493:        if(!image)
1.16      paf       494:                PTHROW(0, 0, 
                    495:                        &method_name, 
1.13      paf       496:                        "does not contain an image");
                    497: 
                    498:        if((params->size()-1)%2) // [I see..] see now?
1.16      paf       499:                PTHROW(0, 0, 
                    500:                        &method_name, 
1.13      paf       501:                        "y coordinate missing");
                    502: 
                    503:        int n=(params->size()-1)/2;
                    504:        
                    505:        gdImage::Point *p=(gdImage::Point *)pool.malloc(sizeof(gdImage::Point)*n);
                    506:        for(int i=0; i<n; i++) {
1.26      parser    507:                p[i].x=r.process(params->get(1+i*2+0)).as_int();
                    508:                p[i].y=r.process(params->get(1+i*2+1)).as_int();
1.13      paf       509:        }
1.16      paf       510:        image->Polygon(p, n, 
1.26      parser    511:                image->Color(r.process(params->get(0)).as_int()));
1.13      paf       512: }
                    513: 
1.17      paf       514: static void _polybar(Request& r, const String& method_name, MethodParams *params) {
1.13      paf       515:        Pool& pool=r.pool();
                    516: 
                    517:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    518:        if(!image)
1.16      paf       519:                PTHROW(0, 0, 
                    520:                        &method_name, 
1.13      paf       521:                        "does not contain an image");
                    522: 
                    523:        if((params->size()-1)%2) // [I see..] see now?
1.16      paf       524:                PTHROW(0, 0, 
                    525:                        &method_name, 
1.13      paf       526:                        "y coordinate missing");
                    527: 
                    528:        int n=(params->size()-1)/2;
                    529:        
                    530:        gdImage::Point *p=(gdImage::Point *)pool.malloc(sizeof(gdImage::Point)*n);
                    531:        for(int i=0; i<n; i++) {
1.26      parser    532:                p[i].x=r.process(params->get(1+i*2+0)).as_int();
                    533:                p[i].y=r.process(params->get(1+i*2+1)).as_int();
1.13      paf       534:        }
1.16      paf       535:        image->FilledPolygon(p, n, 
1.26      parser    536:                image->Color(r.process(params->get(0)).as_int()));
1.12      paf       537: }
                    538: 
1.16      paf       539: // font
                    540: 
                    541: #define Y(y)(y+index*height+1)
1.21      paf       542: 
                    543: /// simple gdImage-based font storage & text output 
1.16      paf       544: class Font : public Pooled {
                    545: public:
                    546:        
1.27      parser    547:        int height;         //< Font heigth
                    548:        int space;          //< Default char width
1.16      paf       549:        gdImage& ifont;
                    550:        const String& alphabet;
                    551:        
                    552:        Font(Pool& pool, 
                    553:                const String& aalphabet, 
                    554:                gdImage& aifont, int aheight, int aspace) : Pooled(pool), 
                    555:                alphabet(aalphabet), 
                    556:                height(aheight), space(aspace), 
                    557:                ifont(aifont) {
                    558:        }
                    559:        
                    560:        /* ******************************** char ********************************** */
                    561:        
                    562:        int index_of(char ch) {
                    563:                if(ch==' ') return -1;
                    564:                return alphabet.pos(&ch, 1);
                    565:        }
                    566:        
                    567:        int index_width(int index) {
                    568:                if(index<0)
                    569:                        index=index_of('.');
                    570:                if(index<0) 
                    571:                        return 0;
                    572:                int tr=ifont.GetTransparent();
                    573:                for(int x=ifont.SX()-1; x>0; x--) {
                    574:                        for(int y=0; y<height-1; y++)
                    575:                                if(ifont.GetPixel(x, Y(y))!=tr) 
                    576:                                        return x+2;
                    577:                }
                    578:                return 0;
                    579:        }
                    580:        
                    581:        void index_display(gdImage& image, int x, int y, int index){
                    582:                if(index>=0) 
                    583:                        ifont.Copy(image, x, y, 0, Y(0), index_width(index), height-1);
                    584:        }
                    585:        
                    586:        /* ******************************** string ********************************** */
                    587:        /*
                    588:        int string_width(const char *cstr){
                    589:                int result=0;
                    590:                for(; *cstr; cstr++)
                    591:                        result+=index_width(index_of(*cstr));
                    592:                return result;
                    593:        }
                    594:        */
                    595:        
                    596:        void string_display(gdImage& image, int x, int y, const String& s){
                    597:                const char *cstr=s.cstr(String::UL_AS_IS);
                    598:                if(cstr) for(; *cstr; cstr++) {
                    599:                        int index=index_of(*cstr);
                    600:                        index_display(image, x, y, index);
                    601:                        x+=space ? space : index_width(index);
                    602:                }
                    603:        }
                    604:        
                    605: };
1.17      paf       606: static void _font(Request& r, const String& method_name, MethodParams *params) {
1.16      paf       607:        Pool& pool=r.pool();
                    608: 
1.30      parser    609:        Value& valphabet=params->as_no_junction(0, "alphabet must not be code");
                    610:        Value& file_name=params->as_no_junction(1, "file_name must not be code");
1.26      parser    611:        int height=r.process(params->get(2)).as_int();
1.16      paf       612: 
1.26      parser    613:        int width=params->size()>3?r.process(params->get(3)).as_int():0;
1.16      paf       614: 
                    615:        static_cast<VImage *>(r.self)->font=new(pool) Font(pool, 
                    616:                valphabet.as_string(), 
                    617:                *load(r, method_name, file_name.as_string()), 
                    618:                height, width);
                    619: }
                    620: 
1.17      paf       621: static void _text(Request& r, const String& method_name, MethodParams *params) {
1.16      paf       622:        Pool& pool=r.pool();
                    623: 
1.26      parser    624:        int x=r.process(params->get(0)).as_int();
                    625:        int y=r.process(params->get(1)).as_int();
1.17      paf       626:        const String& s=r.process(params->get(2)).as_string();
1.16      paf       627: 
                    628:        VImage& vimage=*static_cast<VImage *>(r.self);
                    629:        if(vimage.image)
                    630:                if(vimage.font)
                    631:                        vimage.font->string_display(*vimage.image, x, y, s);
                    632:                else
                    633:                        PTHROW(0, 0,
                    634:                                &method_name,
                    635:                                "set the font first");
                    636:        else
                    637:                PTHROW(0, 0, 
                    638:                        &method_name, 
                    639:                        "does not contain an image");
                    640: }
                    641: 
1.22      paf       642: // constructor
                    643: 
                    644: MImage::MImage(Pool& apool) : Methoded(apool) {
                    645:        set_name(*NEW String(pool(), IMAGE_CLASS_NAME));
                    646: 
                    647: 
1.1       paf       648:        // ^image:measure[DATA]
1.22      paf       649:        add_native_method("measure", Method::CT_DYNAMIC, _measure, 1, 1);
1.3       paf       650: 
1.25      paf       651:        // ^image.html[]
                    652:        // ^image.html[hash]
1.22      paf       653:        add_native_method("html", Method::CT_DYNAMIC, _html, 0, 1);
1.6       paf       654: 
1.25      paf       655:        // ^image.load[background.gif]
1.22      paf       656:        add_native_method("load", Method::CT_DYNAMIC, _load, 1, 1);
1.6       paf       657: 
1.25      paf       658:        // ^image.create[width;height] bgcolor=white
                    659:        // ^image.create[width;height;bgcolor]
1.22      paf       660:        add_native_method("create", Method::CT_DYNAMIC, _create, 2, 3);
1.6       paf       661: 
1.25      paf       662:        // ^image.gif[]
1.22      paf       663:        add_native_method("gif", Method::CT_DYNAMIC, _gif, 0, 0);
1.12      paf       664: 
1.25      paf       665:        // ^image.line(x0;y0;x1;y1;color)
1.22      paf       666:        add_native_method("line", Method::CT_DYNAMIC, _line, 5, 5);
1.13      paf       667: 
1.25      paf       668:        // ^image.fill(x;y;color)
1.22      paf       669:        add_native_method("fill", Method::CT_DYNAMIC, _fill, 3, 3);
1.13      paf       670: 
1.25      paf       671:        // ^image.rectangle(x0;y0;x1;y1;color)
1.22      paf       672:        add_native_method("rectangle", Method::CT_DYNAMIC, _rectangle, 5, 5);
1.13      paf       673: 
1.25      paf       674:        // ^image.bar(x0;y0;x1;y1;color)
1.22      paf       675:        add_native_method("bar", Method::CT_DYNAMIC, _bar, 5, 5);
1.13      paf       676: 
1.25      paf       677:        // ^image.replace(color-source;color-dest)(x;y)... point coord pairs
1.22      paf       678:        add_native_method("replace", Method::CT_DYNAMIC, _replace, 2+3*2, 2+100*2);
1.13      paf       679: 
1.25      paf       680:        // ^image.polygon(color)(x;y)... point coord pairs
1.22      paf       681:        add_native_method("polygon", Method::CT_DYNAMIC, _polygon, 1+3*2, 1+100*2);
1.13      paf       682: 
1.25      paf       683:        // ^image.polybar(color)(x;y)... point coord pairs
1.22      paf       684:        add_native_method("polybar", Method::CT_DYNAMIC, _polybar, 1+3*2, 1+100*2);
1.13      paf       685: 
1.25      paf       686:     // ^image.font[alPHAbet;font-file-name.gif](height)
                    687:     // ^image.font[alPHAbet;font-file-name.gif](height;width)
1.22      paf       688:        add_native_method("font", Method::CT_DYNAMIC, _font, 3, 4);
1.16      paf       689: 
1.25      paf       690:     // ^image.text(x;y)[text]
1.22      paf       691:        add_native_method("text", Method::CT_DYNAMIC, _text, 3, 3);
1.16      paf       692:        
1.22      paf       693: }
                    694: 
                    695: // global variable
                    696: 
                    697: Methoded *image_class;
                    698: 
                    699: // creator
                    700: 
                    701: Methoded *MImage_create(Pool& pool) {
                    702:        return image_class=new(pool) MImage(pool);
1.1       paf       703: }

E-mail: