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

1.1       paf         1: /** @file
                      2:        Parser: @b image parser class.
                      3: 
1.67      paf         4:        Copyright(c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.66      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1       paf         6: 
1.68    ! paf         7:        $Id: image.C,v 1.67 2002/02/08 08:32:33 paf 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)
1.68    ! paf       131:                throw Exception("image.format", 
1.1       paf       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.68    ! paf       137:                throw Exception("image.format", 
1.1       paf       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: 
1.58      parser    145: /// @test remove ugly mech in reader - 20K limit
1.1       paf       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;
1.34      parser    150:        const unsigned char CODE_SIZE_FIRST=0xC0;
                    151:        const unsigned char CODE_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.68    ! paf       156:                throw Exception("image.format", 
1.1       paf       157:                        origin_string, 
1.34      parser    158:                        "not JPEG file - too small");
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.68    ! paf       162:                throw Exception("image.format", 
1.1       paf       163:                        origin_string, 
1.44      parser    164:                        "not JPEG file - wrong signature");
1.31      parser    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: 
1.34      parser    178:                if(head->code >= CODE_SIZE_FIRST && head->code  <= CODE_SIZE_LAST) {
1.31      parser    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.68    ! paf       197:                throw Exception("image.format", 
1.16      paf       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) {
1.60      paf       206:        if(const char *cext=strrchr(file_name.cstr(String::UL_FILE_SPEC), '.')) {
1.1       paf       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
1.68    ! paf       213:                        throw Exception("image.format", 
1.1       paf       214:                                &file_name, 
                    215:                                "unhandled image file name extension '%s'", cext);
                    216:        } else
1.68    ! paf       217:                throw Exception("image.format", 
1.1       paf       218:                        &file_name, 
                    219:                        "can not determine image type - no file name extension");
                    220: }
                    221: 
1.40      parser    222: #ifndef DOXYGEN
1.1       paf       223: struct Read_mem_info {
                    224:        unsigned char *ptr;
                    225:        unsigned char *eof;
                    226: };
1.40      parser    227: #endif
1.1       paf       228: static size_t read_mem(void*& buf, size_t limit, void *info) {
                    229:        Read_mem_info& rmi=*static_cast<Read_mem_info *>(info);
                    230:        buf=rmi.ptr;
                    231:        size_t read_size=min(limit, (size_t)(rmi.eof-rmi.ptr));
                    232:        rmi.ptr+=read_size;
                    233:        return read_size;
                    234: }
                    235: 
1.40      parser    236: #ifndef DOXYGEN
1.1       paf       237: struct Read_disk_info {
                    238:        const String *file_spec;
                    239:        size_t offset;
                    240: };
1.40      parser    241: #endif
1.1       paf       242: static size_t read_disk(void*& buf, size_t limit, void *info) {
                    243:        Read_disk_info& rdi=*static_cast<Read_disk_info *>(info);
                    244:        Pool& pool=rdi.file_spec->pool();
                    245: 
                    246:        size_t read_size;
                    247:        file_read(pool, *rdi.file_spec, 
                    248:                           buf, read_size, 
                    249:                           false/*as_text*/, 
                    250:                           true/*fail_on_read_problem*/, 
                    251:                           rdi.offset, limit);
                    252: 
                    253:        rdi.offset+=read_size;
                    254:        return read_size;
                    255: }
                    256: 
                    257: // methods
                    258: 
1.17      paf       259: static void _measure(Request& r, const String& method_name, MethodParams *params) {
1.1       paf       260:        Pool& pool=r.pool();
                    261: 
1.30      parser    262:        Value& data=params->as_no_junction(0, "data must not be code");
1.1       paf       263: 
1.16      paf       264:        void *info;Measure_reader::Func read_func;
1.1       paf       265:        Read_mem_info read_mem_info;
                    266:        Read_disk_info read_disk_info;
                    267:        const String *file_name;
                    268:        if(data.is_string()) {
                    269:                file_name=data.get_string();
                    270:                read_disk_info.file_spec=&r.absolute(*file_name);
                    271:                read_disk_info.offset=0;
1.16      paf       272:                info=&read_disk_info;read_func=read_disk;
1.1       paf       273:        } else {
                    274:                const VFile& vfile=*data.as_vfile();
                    275:                file_name=&static_cast<Value *>(vfile.fields().get(*name_name))->as_string();
                    276:                read_mem_info.ptr=(unsigned char *)vfile.value_ptr();
                    277:                read_mem_info.eof=read_mem_info.ptr+vfile.value_size();
1.16      paf       278:                info=&read_mem_info;read_func=read_mem;
1.1       paf       279:        }
                    280: 
                    281:        Measure_reader reader(read_func, info);
                    282:        int width, height;
                    283:        measure(pool, *file_name, reader, width, height);
                    284: 
1.6       paf       285:        static_cast<VImage *>(r.self)->set(file_name, width, height);
1.1       paf       286: }
                    287: 
1.40      parser    288: #ifndef DOXYGEN
1.4       paf       289: struct Attrib_info {
1.21      paf       290:        String *tag; ///< html tag being constructed
                    291:        Hash *skip; ///< tag attributes not to append to tag string [to skip]
1.4       paf       292: };
1.40      parser    293: #endif
1.3       paf       294: static void append_attrib_pair(const Hash::Key& key, Hash::Val *val, void *info) {
1.4       paf       295:        Attrib_info& ai=*static_cast<Attrib_info *>(info);
                    296: 
1.49      parser    297:        // skip user-specified and internal(starting with "line-") attributes 
                    298:        if(ai.skip && ai.skip->get(key) || key.pos("line-")==0)
1.4       paf       299:                return;
                    300: 
1.3       paf       301:        Value& value=*static_cast<Value *>(val);
                    302:        // src="a.gif" width=123 ismap[=-1]
1.4       paf       303:        *ai.tag << " " << key;
1.26      parser    304:        if(value.is_string() || value.as_int()>=0)
1.6       paf       305:                *ai.tag << "=\"" << value.as_string() << "\"";
1.3       paf       306: }
1.17      paf       307: static void _html(Request& r, const String& method_name, MethodParams *params) {
1.3       paf       308:        Pool& pool=r.pool();
                    309: 
                    310:        String tag(pool);
                    311:        tag << "<img";
1.4       paf       312: 
1.18      paf       313:        const Hash& fields=static_cast<VImage *>(r.self)->fields();
1.5       paf       314:        Hash *attribs=0;
1.4       paf       315: 
1.39      parser    316:        if(params->size()) {
1.52      parser    317:                Value &vattribs=r.process(params->get(0),
                    318:                        0/*no name*/,
                    319:                        false/*don't intercept string*/);
1.39      parser    320:                if(vattribs.is_defined()) // allow 'void'
1.59      parser    321:                        if(attribs=vattribs.get_hash(&method_name)) {
1.39      parser    322:                                Attrib_info attrib_info={&tag, 0};
                    323:                                attribs->for_each(append_attrib_pair, &attrib_info);
                    324:                        } else
1.68    ! paf       325:                                throw Exception("parser.runtime", 
1.39      parser    326:                                        &method_name, 
                    327:                                        "attributes must be hash");
                    328:        }
1.4       paf       329: 
1.5       paf       330:        Attrib_info attrib_info={&tag, attribs};
1.4       paf       331:        fields.for_each(append_attrib_pair, &attrib_info);
1.6       paf       332:        tag << " />";
1.3       paf       333:        r.write_pass_lang(tag);
                    334: }
1.8       paf       335: 
1.68    ! paf       336: /// @test wrap FILE to auto-object
1.16      paf       337: static gdImage *load(Request& r, const String& method_name, 
                    338:                                         const String& file_name){
                    339:        Pool& pool=r.pool();
                    340: 
1.42      parser    341:        const char *file_name_cstr=r.absolute(file_name).cstr(String::UL_FILE_SPEC);
1.16      paf       342:        if(FILE *f=fopen(file_name_cstr, "rb")) {
                    343:                gdImage& image=*new(pool) gdImage(pool);
1.23      paf       344:                bool ok=image.CreateFromGif(f);
1.16      paf       345:                fclose(f);
1.23      paf       346:                if(!ok)
1.68    ! paf       347:                        throw Exception("image.format", 
1.23      paf       348:                                &file_name,
                    349:                                "is not in GIF format");
1.16      paf       350:                return &image;
                    351:        } else {
1.68    ! paf       352:                throw Exception("file.missing", 
1.16      paf       353:                        &method_name, 
                    354:                        "can not open '%s'", file_name_cstr);
                    355:                return 0;
                    356:        }
                    357: }
                    358: 
                    359: 
1.17      paf       360: static void _load(Request& r, const String& method_name, MethodParams *params) {
1.6       paf       361:        Pool& pool=r.pool();
                    362: 
1.44      parser    363:        const String& file_name=params->as_string(0, "file name must not be code");
1.6       paf       364: 
1.16      paf       365:        gdImage& image=*load(r, method_name, file_name);
                    366:        int width=image.SX();
                    367:        int height=image.SY();
                    368:        static_cast<VImage *>(r.self)->set(&file_name, width, height, &image);
1.6       paf       369: }
                    370: 
1.17      paf       371: static void _create(Request& r, const String& method_name, MethodParams *params) {
1.6       paf       372:        Pool& pool=r.pool();
                    373: 
1.51      parser    374:        int width=params->as_int(0, "width must be int", r);
                    375:        int height=params->as_int(1, "height must be int", r);
1.8       paf       376:        int bgcolor_value=0xffFFff;
1.6       paf       377:        if(params->size()>2)
1.51      parser    378:                bgcolor_value=params->as_int(2, "color must be int", r);
1.15      paf       379:        gdImage& image=*new(pool) gdImage(pool);
                    380:        image.Create(width, height);
                    381:        image.FilledRectangle(0, 0, width-1, height-1, image.Color(bgcolor_value));
                    382:        static_cast<VImage *>(r.self)->set(0, width, height, &image);
1.6       paf       383: }
                    384: 
1.17      paf       385: static void _gif(Request& r, const String& method_name, MethodParams *params) {
1.6       paf       386:        Pool& pool=r.pool();
                    387: 
1.9       paf       388:        gdImage *image=static_cast<VImage *>(r.self)->image;
1.8       paf       389:        if(!image)
1.68    ! paf       390:                throw Exception(0, 
1.16      paf       391:                        &method_name, 
1.12      paf       392:                        "does not contain an image");
1.6       paf       393: 
                    394:        // could _ but don't thing it's wise to use $image.src for vfile.name
1.10      paf       395: 
1.16      paf       396:        String out(pool); image->Gif(out);
1.6       paf       397:        
                    398:        VFile& vfile=*new(pool) VFile(pool);
1.41      parser    399:        Value *content_type=new(pool) VString(*new(pool) String(pool, "image/gif"));
1.20      paf       400:        vfile.set(false/*not tainted*/, 
1.61      paf       401:                out.cstr(), out.size(), 0, content_type);
1.6       paf       402: 
                    403:        r.write_no_lang(vfile);
                    404: }
                    405: 
1.17      paf       406: static void _line(Request& r, const String& method_name, MethodParams *params) {
1.12      paf       407:        Pool& pool=r.pool();
                    408: 
                    409:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    410:        if(!image)
1.68    ! paf       411:                throw Exception(0, 
1.16      paf       412:                        &method_name, 
1.12      paf       413:                        "does not contain an image");
                    414: 
                    415:        image->Line(
1.51      parser    416:                params->as_int(0, "x0 must be int", r), 
                    417:                params->as_int(1, "y0 must be int", r), 
                    418:                params->as_int(2, "x1 must be int", r), 
                    419:                params->as_int(3, "y1 must be int", r), 
                    420:                image->Color(params->as_int(4, "color must be int", r)));
1.13      paf       421: }
                    422: 
1.17      paf       423: static void _fill(Request& r, const String& method_name, MethodParams *params) {
1.13      paf       424:        Pool& pool=r.pool();
                    425: 
                    426:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    427:        if(!image)
1.68    ! paf       428:                throw Exception(0, 
1.16      paf       429:                        &method_name, 
1.13      paf       430:                        "does not contain an image");
1.12      paf       431: 
1.13      paf       432:        image->Fill(
1.51      parser    433:                params->as_int(0, "x must be int", r), 
                    434:                params->as_int(1, "y must be int", r), 
                    435:                image->Color(params->as_int(2, "color must be int", r)));
1.13      paf       436: }
                    437: 
1.17      paf       438: static void _rectangle(Request& r, const String& method_name, MethodParams *params) {
1.13      paf       439:        Pool& pool=r.pool();
                    440: 
                    441:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    442:        if(!image)
1.68    ! paf       443:                throw Exception(0, 
1.16      paf       444:                        &method_name, 
1.13      paf       445:                        "does not contain an image");
                    446: 
                    447:        image->Rectangle(
1.51      parser    448:                params->as_int(0, "x0 must be int", r), 
                    449:                params->as_int(1, "y0 must be int", r), 
                    450:                params->as_int(2, "x1 must be int", r), 
                    451:                params->as_int(3, "y1 must be int", r), 
                    452:                image->Color(params->as_int(4, "color must be int", r)));
1.13      paf       453: }
                    454: 
1.17      paf       455: static void _bar(Request& r, const String& method_name, MethodParams *params) {
1.13      paf       456:        Pool& pool=r.pool();
                    457: 
                    458:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    459:        if(!image)
1.68    ! paf       460:                throw Exception(0, 
1.16      paf       461:                        &method_name, 
1.13      paf       462:                        "does not contain an image");
                    463: 
                    464:        image->FilledRectangle(
1.51      parser    465:                params->as_int(0, "x0 must be int", r), 
                    466:                params->as_int(1, "y0 must be int", r), 
                    467:                params->as_int(2, "x1 must be int", r), 
                    468:                params->as_int(3, "y1 must be int", r), 
                    469:                image->Color(params->as_int(4, "color must be int", r)));
1.13      paf       470: }
                    471: 
1.44      parser    472: #ifndef DOXYGEN
                    473: static void add_point(Array::Item *value, void *info) {
                    474:        Array& row=*static_cast<Array *>(value);
                    475:        gdImage::Point **p=static_cast<gdImage::Point **>(info);
                    476:        
                    477:        (**p).x=row.get_string(0)->as_int();
                    478:        (**p).y=row.get_string(1)->as_int();
                    479:        (*p)++;
                    480: }
                    481: #endif
1.17      paf       482: static void _replace(Request& r, const String& method_name, MethodParams *params) {
1.13      paf       483:        Pool& pool=r.pool();
                    484: 
                    485:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    486:        if(!image)
1.68    ! paf       487:                throw Exception(0, 
1.16      paf       488:                        &method_name, 
1.13      paf       489:                        "does not contain an image");
                    490: 
1.44      parser    491:        Table *table=params->as_no_junction(2, "coordinates must not be code").get_table();
                    492:        if(!table) 
1.68    ! paf       493:                throw Exception(0,
1.44      parser    494:                        &method_name,
                    495:                        "coordinates must be table");
1.13      paf       496: 
1.44      parser    497:        gdImage::Point *all_p=(gdImage::Point *)pool.malloc(sizeof(gdImage::Point)*table->size());
                    498:        gdImage::Point *add_p=all_p;    
                    499:        table->for_each(add_point, &add_p);
                    500:        image->FilledPolygonReplaceColor(all_p, table->size(), 
1.51      parser    501:                image->Color(params->as_int(0, "src color must be int", r)),
                    502:                image->Color(params->as_int(1, "dest color must be int", r)));
1.13      paf       503: }
                    504: 
1.44      parser    505: static void _polyline(Request& r, const String& method_name, MethodParams *params) {
1.13      paf       506:        Pool& pool=r.pool();
                    507: 
                    508:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    509:        if(!image)
1.68    ! paf       510:                throw Exception(0, 
1.16      paf       511:                        &method_name, 
1.13      paf       512:                        "does not contain an image");
                    513: 
1.44      parser    514:        Table *table=params->as_no_junction(1, "coordinates must not be code").get_table();
                    515:        if(!table) 
1.68    ! paf       516:                throw Exception(0,
1.44      parser    517:                        &method_name,
                    518:                        "coordinates must be table");
                    519: 
                    520:        gdImage::Point *all_p=(gdImage::Point *)pool.malloc(sizeof(gdImage::Point)*table->size());
                    521:        gdImage::Point *add_p=all_p;    
                    522:        table->for_each(add_point, &add_p);
                    523:        image->Polygon(all_p, table->size(), 
1.51      parser    524:                image->Color(params->as_int(0, "color must be int", r)),
1.44      parser    525:                false/*not closed*/);
                    526: }
                    527: 
                    528: static void _polygon(Request& r, const String& method_name, MethodParams *params) {
                    529:        Pool& pool=r.pool();
                    530: 
                    531:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    532:        if(!image)
1.68    ! paf       533:                throw Exception(0, 
1.16      paf       534:                        &method_name, 
1.44      parser    535:                        "does not contain an image");
1.13      paf       536: 
1.44      parser    537:        Table *table=params->as_no_junction(1, "coordinates must not be code").get_table();
                    538:        if(!table) 
1.68    ! paf       539:                throw Exception(0,
1.44      parser    540:                        &method_name,
                    541:                        "coordinates must be table");
                    542: 
                    543:        gdImage::Point *all_p=(gdImage::Point *)pool.malloc(sizeof(gdImage::Point)*table->size());
                    544:        gdImage::Point *add_p=all_p;    
                    545:        table->for_each(add_point, &add_p);
                    546:        image->Polygon(all_p, table->size(), 
1.51      parser    547:                image->Color(params->as_int(0, "color must be int", r)));
1.13      paf       548: }
                    549: 
1.17      paf       550: static void _polybar(Request& r, const String& method_name, MethodParams *params) {
1.13      paf       551:        Pool& pool=r.pool();
                    552: 
                    553:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    554:        if(!image)
1.68    ! paf       555:                throw Exception(0, 
1.16      paf       556:                        &method_name, 
1.13      paf       557:                        "does not contain an image");
                    558: 
1.44      parser    559:        Table *table=params->as_no_junction(1, "coordinates must not be code").get_table();
                    560:        if(!table) 
1.68    ! paf       561:                throw Exception("parser.runtime",
1.44      parser    562:                        &method_name,
                    563:                        "coordinates must be table");
1.13      paf       564: 
1.44      parser    565:        gdImage::Point *all_p=(gdImage::Point *)pool.malloc(sizeof(gdImage::Point)*table->size());
                    566:        gdImage::Point *add_p=all_p;    
                    567:        table->for_each(add_point, &add_p);
                    568:        image->FilledPolygon(all_p, table->size(), 
1.51      parser    569:                image->Color(params->as_int(0, "color must be int", r)));
1.12      paf       570: }
                    571: 
1.16      paf       572: // font
                    573: 
                    574: #define Y(y)(y+index*height+1)
1.21      paf       575: 
                    576: /// simple gdImage-based font storage & text output 
1.16      paf       577: class Font : public Pooled {
                    578: public:
                    579:        
1.38      parser    580:        const static int letter_spacing;
1.35      parser    581:        int height;         ///< Font heigth
                    582:        int monospace;      ///< Default char width
                    583:        int spacebarspace; ///< spacebar width
1.16      paf       584:        gdImage& ifont;
                    585:        const String& alphabet;
                    586:        
                    587:        Font(Pool& pool, 
                    588:                const String& aalphabet, 
1.35      parser    589:                gdImage& aifont, int aheight, int amonospace, int aspacebarspace) : Pooled(pool), 
1.16      paf       590:                alphabet(aalphabet), 
1.35      parser    591:                height(aheight), monospace(amonospace),  spacebarspace(aspacebarspace),
1.16      paf       592:                ifont(aifont) {
                    593:        }
                    594:        
                    595:        /* ******************************** char ********************************** */
                    596:        
                    597:        int index_of(char ch) {
                    598:                if(ch==' ') return -1;
                    599:                return alphabet.pos(&ch, 1);
                    600:        }
                    601:        
                    602:        int index_width(int index) {
                    603:                if(index<0)
1.35      parser    604:                        return spacebarspace;
1.16      paf       605:                int tr=ifont.GetTransparent();
1.35      parser    606:                for(int x=ifont.SX()-1; x>=0; x--) {
1.16      paf       607:                        for(int y=0; y<height-1; y++)
                    608:                                if(ifont.GetPixel(x, Y(y))!=tr) 
1.35      parser    609:                                        return x+1;
1.16      paf       610:                }
                    611:                return 0;
                    612:        }
                    613:        
                    614:        void index_display(gdImage& image, int x, int y, int index){
                    615:                if(index>=0) 
                    616:                        ifont.Copy(image, x, y, 0, Y(0), index_width(index), height-1);
                    617:        }
                    618:        
                    619:        /* ******************************** string ********************************** */
1.47      parser    620:        
                    621:        int string_width(const String& s){
1.61      paf       622:                const char *cstr=s.cstr();
1.16      paf       623:                int result=0;
                    624:                for(; *cstr; cstr++)
                    625:                        result+=index_width(index_of(*cstr));
                    626:                return result;
                    627:        }
                    628:        
                    629:        void string_display(gdImage& image, int x, int y, const String& s){
1.61      paf       630:                const char *cstr=s.cstr();
1.16      paf       631:                if(cstr) for(; *cstr; cstr++) {
                    632:                        int index=index_of(*cstr);
                    633:                        index_display(image, x, y, index);
1.38      parser    634:                        x+=letter_spacing + (monospace ? monospace : index_width(index));
1.16      paf       635:                }
                    636:        }
                    637:        
                    638: };
1.38      parser    639: const int Font::letter_spacing=1;
1.35      parser    640: 
1.17      paf       641: static void _font(Request& r, const String& method_name, MethodParams *params) {
1.16      paf       642:        Pool& pool=r.pool();
                    643: 
1.37      parser    644:        const String& alphabet=params->as_string(0, "alphabet must not be code");
                    645:        gdImage& image=*load(r, method_name, params->as_string(1, "file_name must not be code"));
1.51      parser    646:        int spacebar_width=params->as_int(2, "spacebar_width must be int", r);
1.37      parser    647:        int monospace_width;
                    648:        if(params->size()>3) {
1.51      parser    649:                monospace_width=params->as_int(3, "monospace_width must be int", r);
1.37      parser    650:                if(!monospace_width)
                    651:                        monospace_width=image.SX();
                    652:        } else
                    653:                monospace_width=0;
1.16      paf       654: 
1.37      parser    655:        if(!alphabet.size())
1.68    ! paf       656:                throw Exception("parser.runtime",
1.37      parser    657:                        &method_name,
                    658:                        "alphabet must not be empty");
                    659:        
1.16      paf       660:        static_cast<VImage *>(r.self)->font=new(pool) Font(pool, 
1.37      parser    661:                alphabet, 
1.36      parser    662:                image, 
1.37      parser    663:                image.SY() / alphabet.size(), monospace_width, spacebar_width);
1.16      paf       664: }
                    665: 
1.17      paf       666: static void _text(Request& r, const String& method_name, MethodParams *params) {
1.16      paf       667:        Pool& pool=r.pool();
                    668: 
1.51      parser    669:        int x=params->as_int(0, "x must be int", r);
                    670:        int y=params->as_int(1, "y must be int", r);
1.36      parser    671:        const String& s=params->as_string(2, "text must not be code");
1.16      paf       672: 
                    673:        VImage& vimage=*static_cast<VImage *>(r.self);
                    674:        if(vimage.image)
                    675:                if(vimage.font)
                    676:                        vimage.font->string_display(*vimage.image, x, y, s);
                    677:                else
1.68    ! paf       678:                        throw Exception("parser.runtime",
1.16      paf       679:                                &method_name,
                    680:                                "set the font first");
                    681:        else
1.68    ! paf       682:                throw Exception(0, 
1.16      paf       683:                        &method_name, 
                    684:                        "does not contain an image");
                    685: }
                    686: 
1.47      parser    687: static void _length(Request& r, const String& method_name, MethodParams *params) {
                    688:        Pool& pool=r.pool();
                    689: 
                    690:        const String& s=params->as_string(0, "text must not be code");
                    691: 
                    692:        VImage& vimage=*static_cast<VImage *>(r.self);
                    693:        if(vimage.image)
                    694:                if(vimage.font) {
                    695:                        VInt& result=*new(pool) VInt(pool, vimage.font->string_width(s));
                    696:                        result.set_name(method_name);
                    697:                        r.write_assign_lang(result);
                    698:                } else
1.68    ! paf       699:                        throw Exception("parser.runtime",
1.47      parser    700:                                &method_name,
                    701:                                "set the font first");
                    702:        else
1.68    ! paf       703:                throw Exception(0, 
1.47      parser    704:                        &method_name, 
                    705:                        "does not contain an image");
                    706: }
                    707: 
1.48      parser    708: static void _arc(Request& r, const String& method_name, MethodParams *params) {
                    709:        Pool& pool=r.pool();
                    710: 
                    711:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    712:        if(!image)
1.68    ! paf       713:                throw Exception(0, 
1.48      parser    714:                        &method_name, 
                    715:                        "does not contain an image");
                    716: 
                    717:        image->Arc(
1.51      parser    718:                params->as_int(0, "center_x must be int", r), 
                    719:                params->as_int(1, "center_y must be int", r), 
                    720:                params->as_int(2, "width must be int", r), 
                    721:                params->as_int(3, "height must be int", r), 
                    722:                params->as_int(4, "start degrees must be int", r), 
                    723:                params->as_int(5, "end degrees must be int", r), 
                    724:                image->Color(params->as_int(6, "cx must be int", r)));
1.48      parser    725: }
                    726: 
1.49      parser    727: static void _sector(Request& r, const String& method_name, MethodParams *params) {
                    728:        Pool& pool=r.pool();
                    729: 
                    730:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    731:        if(!image)
1.68    ! paf       732:                throw Exception(0, 
1.49      parser    733:                        &method_name, 
                    734:                        "does not contain an image");
                    735: 
                    736:        image->Sector(
1.51      parser    737:                params->as_int(0, "center_x must be int", r), 
                    738:                params->as_int(1, "center_y must be int", r), 
                    739:                params->as_int(2, "width must be int", r), 
                    740:                params->as_int(3, "height must be int", r), 
                    741:                params->as_int(4, "start degrees must be int", r), 
                    742:                params->as_int(5, "end degrees must be int", r), 
                    743:                image->Color(params->as_int(6, "color must be int", r)));
1.49      parser    744: }
                    745: 
1.48      parser    746: static void _circle(Request& r, const String& method_name, MethodParams *params) {
                    747:        Pool& pool=r.pool();
                    748: 
                    749:        gdImage *image=static_cast<VImage *>(r.self)->image;
                    750:        if(!image)
1.68    ! paf       751:                throw Exception(0, 
1.48      parser    752:                        &method_name, 
                    753:                        "does not contain an image");
                    754: 
1.51      parser    755:        int size=params->as_int(2, "radius must be int", r)*2;
1.48      parser    756:        image->Arc(
1.51      parser    757:                params->as_int(0, "center_x must be int", r), 
                    758:                params->as_int(1, "center_y must be int", r), 
1.50      parser    759:                size, //w
                    760:                size, //h
1.48      parser    761:                0, //s
                    762:                360, //e
1.51      parser    763:                image->Color(params->as_int(3, "color must be int", r)));
1.48      parser    764: }
                    765: 
1.53      parser    766: gdImage& as_image(Pool& pool, const String& method_name, MethodParams *params, 
                    767:                                                int index, const char *msg) {
                    768:        Value& value=params->as_no_junction(index, msg);
                    769: 
                    770:        if(strcmp(value.type(), VIMAGE_TYPE)!=0)
1.68    ! paf       771:                throw Exception("parser.runtime", 
1.53      parser    772:                        &method_name, 
                    773:                        msg);
                    774: 
                    775:        gdImage *src=static_cast<VImage *>(&value)->image;
                    776:        if(!src)
1.68    ! paf       777:                throw Exception("parser.runtime", 
1.53      parser    778:                        &method_name, 
                    779:                        msg);
                    780: 
                    781:        return *src;
                    782: }
                    783: 
                    784: static void _copy(Request& r, const String& method_name, MethodParams *params) {
                    785:        Pool& pool=r.pool();
                    786: 
                    787:        gdImage *dest=static_cast<VImage *>(r.self)->image;
                    788:        if(!dest)
1.68    ! paf       789:                throw Exception(0, 
1.53      parser    790:                        &method_name, 
                    791:                        "self does not contain an image");
                    792: 
                    793:        gdImage& src=as_image(pool, method_name, params, 0, "src must be image");
                    794: 
                    795:        int sx=params->as_int(1, "src_x must be int", r);
                    796:        int sy=params->as_int(2, "src_y must be int", r);
                    797:        int sw=params->as_int(3, "src_w must be int", r);
                    798:        int sh=params->as_int(4, "src_h must be int", r);
                    799:        int dx=params->as_int(5, "dest_x must be int", r);
                    800:        int dy=params->as_int(6, "dest_y must be int", r);
                    801:        if(params->size()>1+2+2+2) {
                    802:                int dw=params->as_int(1+2+2+2, "dest_w must be int", r);
1.56      parser    803:                int dh=(int)(params->size()>1+2+2+2+1?
                    804:                        params->as_int(1+2+2+2+1, "dest_h must be int", r):sh*(((double)dw)/((double)sw)));
                    805:                int tolerance=params->size()>1+2+2+2+2?
                    806:                        params->as_int(1+2+2+2+2, "tolerance must be int", r):150;
1.53      parser    807: 
1.56      parser    808:                src.CopyResampled(*dest, dx, dy, sx, sy, dw, dh, sw, sh, tolerance);
1.53      parser    809:        } else
1.54      parser    810:                src.Copy(*dest, dx, dy, sx, sy, sw, sh);
1.53      parser    811: }
                    812: 
                    813: 
1.22      paf       814: // constructor
                    815: 
                    816: MImage::MImage(Pool& apool) : Methoded(apool) {
                    817:        set_name(*NEW String(pool(), IMAGE_CLASS_NAME));
                    818: 
                    819: 
1.1       paf       820:        // ^image:measure[DATA]
1.22      paf       821:        add_native_method("measure", Method::CT_DYNAMIC, _measure, 1, 1);
1.3       paf       822: 
1.25      paf       823:        // ^image.html[]
                    824:        // ^image.html[hash]
1.22      paf       825:        add_native_method("html", Method::CT_DYNAMIC, _html, 0, 1);
1.6       paf       826: 
1.25      paf       827:        // ^image.load[background.gif]
1.22      paf       828:        add_native_method("load", Method::CT_DYNAMIC, _load, 1, 1);
1.6       paf       829: 
1.25      paf       830:        // ^image.create[width;height] bgcolor=white
                    831:        // ^image.create[width;height;bgcolor]
1.22      paf       832:        add_native_method("create", Method::CT_DYNAMIC, _create, 2, 3);
1.6       paf       833: 
1.25      paf       834:        // ^image.gif[]
1.22      paf       835:        add_native_method("gif", Method::CT_DYNAMIC, _gif, 0, 0);
1.12      paf       836: 
1.25      paf       837:        // ^image.line(x0;y0;x1;y1;color)
1.22      paf       838:        add_native_method("line", Method::CT_DYNAMIC, _line, 5, 5);
1.13      paf       839: 
1.25      paf       840:        // ^image.fill(x;y;color)
1.22      paf       841:        add_native_method("fill", Method::CT_DYNAMIC, _fill, 3, 3);
1.13      paf       842: 
1.25      paf       843:        // ^image.rectangle(x0;y0;x1;y1;color)
1.22      paf       844:        add_native_method("rectangle", Method::CT_DYNAMIC, _rectangle, 5, 5);
1.13      paf       845: 
1.25      paf       846:        // ^image.bar(x0;y0;x1;y1;color)
1.22      paf       847:        add_native_method("bar", Method::CT_DYNAMIC, _bar, 5, 5);
1.13      paf       848: 
1.44      parser    849:        // ^image.replace(color-source;color-dest)[table x:y]
                    850:        add_native_method("replace", Method::CT_DYNAMIC, _replace, 3, 3);
                    851: 
                    852:        // ^image.polyline(color)[table x:y]
                    853:        add_native_method("polyline", Method::CT_DYNAMIC, _polyline, 2, 2);
1.13      paf       854: 
1.44      parser    855:        // ^image.polygon(color)[table x:y]
                    856:        add_native_method("polygon", Method::CT_DYNAMIC, _polygon, 2, 2);
1.13      paf       857: 
1.44      parser    858:        // ^image.polybar(color)[table x:y]
                    859:        add_native_method("polybar", Method::CT_DYNAMIC, _polybar, 2, 2);
1.13      paf       860: 
1.36      parser    861:     // ^image.font[alPHAbet;font-file-name.gif](spacebar_width)
                    862:     // ^image.font[alPHAbet;font-file-name.gif](spacebar_width;width)
                    863:        add_native_method("font", Method::CT_DYNAMIC, _font, 3, 4);
1.16      paf       864: 
1.25      paf       865:     // ^image.text(x;y)[text]
1.22      paf       866:        add_native_method("text", Method::CT_DYNAMIC, _text, 3, 3);
1.47      parser    867:        
                    868:     // ^image.ngth[text]
                    869:        add_native_method("length", Method::CT_DYNAMIC, _length, 1, 1);
1.16      paf       870:        
1.48      parser    871:        // ^image.arc(center x;center y;width;height;start in degrees;end in degrees;color)
                    872:        add_native_method("arc", Method::CT_DYNAMIC, _arc, 7, 7);
1.49      parser    873: 
                    874:        // ^image.sector(center x;center y;width;height;start in degrees;end in degrees;color)
                    875:        add_native_method("sector", Method::CT_DYNAMIC, _sector, 7, 7);
1.48      parser    876: 
                    877:        // ^image.circle(center x;center y;r;color)
                    878:        add_native_method("circle", Method::CT_DYNAMIC, _circle, 4, 4);
                    879: 
1.56      parser    880:        // ^image.copy[source](src x;src y;src w;src h;dst x;dst y[;dest w[;dest h[;tolerance]]])
                    881:        add_native_method("copy", Method::CT_DYNAMIC, _copy, 1+2+2+2, (1+2+2+2)+2+1);
1.22      paf       882: }
                    883: 
                    884: // global variable
                    885: 
                    886: Methoded *image_class;
                    887: 
                    888: // creator
                    889: 
                    890: Methoded *MImage_create(Pool& pool) {
                    891:        return image_class=new(pool) MImage(pool);
1.1       paf       892: }

E-mail: