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

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

E-mail: