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

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

E-mail: