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

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.169   ! moko       29: volatile const char * IDENT_IMAGE_C="$Id: image.C,v 1.168 2020/11/30 16:44:24 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: 
1.169   ! moko      231: ///*********************************************** support functions
1.91      paf       232: 
1.1       paf       233: class Measure_reader {
                    234: public:
1.91      paf       235:        virtual size_t read(const char* &buf, size_t limit)=0;
1.79      paf       236:        virtual void seek(long value, int whence)=0;
                    237:        virtual long tell()=0;
1.77      paf       238: };
1.1       paf       239: 
1.77      paf       240: class Measure_file_reader: public Measure_reader {
1.152     moko      241:        const String& file_name;
1.91      paf       242:        int f;
                    243: 
1.77      paf       244: public:
1.152     moko      245:        Measure_file_reader(int af, const String& afile_name):
                    246:                file_name(afile_name), f(af) {
1.1       paf       247:        }
                    248: 
1.91      paf       249:        override size_t read(const char* &abuf, size_t limit) {
1.77      paf       250:                if(limit==0)
1.1       paf       251:                        return 0;
1.77      paf       252: 
1.91      paf       253:                char* lbuf=new(PointerFreeGC) char[limit];
1.143     moko      254:                ssize_t read_size=::read(f, lbuf, limit);  abuf=lbuf;
                    255:                if(read_size<0)
                    256:                        throw Exception(0, &file_name, "measure read failed: %s (%d)", strerror(errno), errno);
1.1       paf       257:                return read_size;
                    258:        }
                    259: 
1.91      paf       260:        override void seek(long value, int whence) {
1.79      paf       261:                if(lseek(f, value, whence)<0)
1.152     moko      262:                        throw Exception(IMAGE_FORMAT, &file_name, "seek(value=%ld, whence=%d) failed: %s (%d)", value, whence, strerror(errno), errno);
1.77      paf       263:        }
                    264: 
1.91      paf       265:        override long tell() { return lseek(f, 0, SEEK_CUR); }
1.79      paf       266: 
1.77      paf       267: };
                    268: 
                    269: class Measure_buf_reader: public Measure_reader {
1.91      paf       270: 
                    271:        const char* buf; size_t size;
                    272:        const String& file_name; 
                    273: 
                    274:        size_t offset;
                    275: 
1.77      paf       276: public:
1.91      paf       277:        Measure_buf_reader(const char* abuf, size_t asize, const String& afile_name): 
1.77      paf       278:                buf(abuf), size(asize), file_name(afile_name), offset(0) {
                    279:        }
                    280:        
1.91      paf       281:        override size_t read(const char* &abuf, size_t limit) {
1.77      paf       282:                size_t to_read=min(limit, size-offset);
1.91      paf       283:                abuf=buf+offset;
1.77      paf       284:                offset+=to_read;
                    285:                return to_read;
                    286:        }
                    287: 
1.91      paf       288:        override void seek(long value, int whence) {
1.79      paf       289:                size_t new_offset;
                    290:                switch(whence) {
                    291:                case SEEK_CUR: new_offset=offset+value; break;
                    292:                case SEEK_SET: new_offset=(size_t)value; break;
1.164     moko      293:                case SEEK_END: new_offset=size; break;
1.91      paf       294:                default: 
1.155     moko      295:                        throw Exception(0, 0, "whence #%d not supported", 0, whence); 
1.91      paf       296:                        break; // never
1.79      paf       297:                }
                    298:                
1.77      paf       299:                if((ssize_t)new_offset<0 || new_offset>size)
1.155     moko      300:                        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       301:                                        value, whence, new_offset, size);
1.77      paf       302:                offset=new_offset;
                    303:        }
                    304: 
1.91      paf       305:        override long tell() { return offset; }
1.79      paf       306: 
1.1       paf       307: };
1.77      paf       308: 
1.1       paf       309: 
1.169   ! moko      310: struct Measure_info {
        !           311:        ushort width;
        !           312:        ushort height;
        !           313:        Value** exif;
        !           314:        Value** xmp;
        !           315:        Charset* xmp_charset;
1.72      paf       316: };
                    317: 
1.164     moko      318: 
1.169   ! moko      319: inline ushort x_endian_to_ushort(uchar b0, uchar b1) {
        !           320:        return (ushort)((b1<<8) + b0);
        !           321: }
        !           322: 
        !           323: inline uint x_endian_to_uint(uchar b0, uchar b1, uchar b2, uchar b3) {
        !           324:        return (uint)(((((b3<<8) + b2)<<8)+b1)<<8)+b0;
        !           325: }
        !           326: 
        !           327: inline ushort endian_to_ushort(bool is_big, const uchar *b/* [2] */) {
        !           328:        return is_big ? x_endian_to_ushort(b[1], b[0]) : x_endian_to_ushort(b[0], b[1]);
        !           329: }
        !           330: 
        !           331: inline uint endian_to_uint(bool is_big, const uchar *b /* [4] */) {
        !           332:        return is_big ? x_endian_to_uint(b[3], b[2], b[1], b[0]) : x_endian_to_uint(b[0], b[1], b[2], b[3]);
        !           333: }
1.1       paf       334: 
1.169   ! moko      335: ///*********************************************** JPEG
1.31      parser    336: struct JPG_Segment_head {
1.80      paf       337:        uchar marker;
                    338:        uchar code;
                    339:        uchar length[2];
1.1       paf       340: };
1.21      paf       341: /// JPEG frame header
1.31      parser    342: struct JPG_Size_segment_body {
1.164     moko      343:        char data;              //< data precision of bits/sample
                    344:        uchar height[2];        //< image height
                    345:        uchar width[2];         //< image width
                    346:        char numComponents;     //< number of color components
1.1       paf       347: };
                    348: 
1.80      paf       349: /// JPEG Exif TIFF Header
                    350: struct JPG_Exif_TIFF_header {
1.165     moko      351:        char byte_align_identifier[2];
                    352:        uchar signature[2]; // always 000A [or 0A00]
1.80      paf       353:        uchar first_IFD_offset[4]; // Usually the first IFD starts immediately next to TIFF header, so this offset has value '0x00000008'.
                    354: };
                    355: 
                    356: // JPEG Exif IFD start
1.91      paf       357: struct JPG_Exif_IFD_begin {
1.80      paf       358:        uchar directory_entry_count[2]; // the number of directory entry contains in this IFD
                    359: };
                    360: 
                    361: // TTTT ffff NNNNNNNN DDDDDDDD 
                    362: struct JPG_Exif_IFD_entry {
                    363:        uchar tag[2]; // Tag number, this shows a kind of data
                    364:        uchar format[2]; // data format
                    365:        uchar components_count[4]; // number of components
                    366:        uchar value_or_offset_to_it[4]; // data value or offset to data value
                    367: };
                    368: 
                    369: #define JPG_IFD_TAG_EXIF_OFFSET 0x8769
                    370: 
1.118     misha     371: #define JPG_IFD_TAG_EXIF_GPS_OFFSET 0x8825
                    372: 
1.89      paf       373: #define JPEG_EXIF_DATE_CHARS 20
                    374: 
1.155     moko      375: static Value* parse_IFD_entry_formatted_one_value(bool is_big, ushort format, size_t component_size, const uchar *value) {
1.80      paf       376:        switch(format) {
                    377:        case 1: // unsigned byte
1.91      paf       378:                return new VInt((uchar)value[0]);
1.80      paf       379:        case 3: // unsigned short
1.91      paf       380:                return new VInt(endian_to_ushort(is_big, value));
1.80      paf       381:        case 4: // unsigned long
                    382:                 // 'double' because parser's Int is signed
1.91      paf       383:                return new VDouble(endian_to_uint(is_big, value));
1.80      paf       384:        case 5: // unsigned rational
                    385:                {
                    386:                        uint numerator=endian_to_uint(is_big, value); value+=component_size/2;
                    387:                        uint denominator=endian_to_uint(is_big, value);
                    388:                        if(!denominator)
                    389:                                return 0;
1.91      paf       390:                        return new VDouble(((double)numerator)/denominator);
1.80      paf       391:                }
                    392:        case 6: // signed byte
1.91      paf       393:                return new VInt((signed char)value[0]);
1.80      paf       394:        case 8: // signed short
1.91      paf       395:                return new VInt((signed short)endian_to_ushort(is_big, value));
1.80      paf       396:        case 9: // signed long
1.91      paf       397:                return new VInt((signed int)endian_to_uint(is_big, value));
1.80      paf       398:        case 10: // signed rational
                    399:                {
                    400:                        signed int numerator=(signed int)endian_to_uint(is_big, value); value+=component_size/2;
                    401:                        uint denominator=endian_to_uint(is_big, value);
                    402:                        if(!denominator)
                    403:                                return 0;
1.91      paf       404:                        return new VDouble(numerator/denominator);
1.80      paf       405:                }
                    406:                /*
                    407:        case 11: // single float
1.120     misha     408:                @todo
1.80      paf       409:        case 12: // double float
1.120     misha     410:                @todo
1.80      paf       411:                */
                    412:        };      
                    413:        
                    414:        return 0;
                    415: }
                    416: 
1.89      paf       417: // date.C
1.147     moko      418: tm cstr_to_time_t(char *, const char **);
1.89      paf       419: 
1.155     moko      420: 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       421:        if(format==2) { // ascii string, exception: the only type with varying size
1.91      paf       422:                const char* cstr=(const char* )value;
                    423:                size_t length=components_count;
1.89      paf       424:                // Data format is "YYYY:MM:DD HH:MM:SS"+0x00, total 20bytes
1.155     moko      425:                if(length==JPEG_EXIF_DATE_CHARS && isdigit((unsigned char)cstr[0]) && cstr[length-1]==0) {
                    426:                        char cstr_writable[JPEG_EXIF_DATE_CHARS];
1.89      paf       427:                        strcpy(cstr_writable, cstr);
                    428: 
1.106     paf       429:                        try {
1.147     moko      430:                                tm tmIn=cstr_to_time_t(cstr_writable, 0);
1.146     moko      431:                                return new VDate(tmIn);
1.106     paf       432:                        }
                    433:                        catch(...) { /*ignore bad date times*/ }
1.89      paf       434:                }
                    435: 
1.123     misha     436:                return new VString(*new String(cstr, String::L_TAINTED));
1.80      paf       437:        }
                    438: 
                    439:        if(components_count==1)
1.91      paf       440:                return parse_IFD_entry_formatted_one_value(is_big, format, component_size, value);
1.80      paf       441: 
1.91      paf       442:        VHash* result=new VHash;
                    443:        HashStringValue& hash=result->hash();
1.80      paf       444:        for(uint i=0; i<components_count; i++, value+=component_size) {
1.91      paf       445:                hash.put(
1.93      paf       446:                        String::Body::Format(i),
1.91      paf       447:                        parse_IFD_entry_formatted_one_value(is_big, format, component_size, value));
1.80      paf       448:        }
                    449: 
1.91      paf       450:        return result;
1.80      paf       451: }
                    452: 
1.155     moko      453: static Value* parse_IFD_entry_value(bool is_big, Measure_reader& reader, long tiff_base, JPG_Exif_IFD_entry& entry) {
1.80      paf       454:        size_t format2component_size[]={
                    455:                0, // undefined
                    456:                1, // unsigned byte
                    457:                1, // ascii string
                    458:                2, // unsigned short
                    459:                4, // unsigned long
                    460:                8, // unsigned rational
                    461:                1, // signed byte
                    462:                0, // undefined
                    463:                2, // signed short
                    464:                4, // signed long
                    465:                8, // signed rational
                    466:                /*
                    467:                4, // single float
                    468:                8, // double float
                    469:                */
                    470:        };
                    471: 
                    472:        ushort format=endian_to_ushort(is_big, entry.format);
                    473:        if(format>=sizeof(format2component_size)/sizeof(format2component_size[0]))
                    474:                return 0; // format out of range, ignoring
                    475: 
                    476:        size_t component_size=format2component_size[format];
                    477:        if(component_size==0)
                    478:                return 0; // undefined format
                    479: 
                    480:        // You can get the total data byte length by multiplies 
                    481:        // a 'bytes/components' value (see above chart) by number of components stored 'NNNNNNNN' area
                    482:        uint components_count=endian_to_uint(is_big, entry.components_count);
1.121     misha     483:        uint value_size=component_size*components_count;
1.80      paf       484:        // If its size is over 4bytes, 'DDDDDDDD' contains the offset to data stored address
1.91      paf       485:        Value* result;
1.80      paf       486: 
                    487:        if(value_size<=4)
1.155     moko      488:                result=parse_IFD_entry_formatted_value(is_big, format, component_size, components_count, entry.value_or_offset_to_it);
1.80      paf       489:        else {
                    490:                long remembered=reader.tell();
                    491:                {
                    492:                        reader.seek(tiff_base+endian_to_uint(is_big, entry.value_or_offset_to_it), SEEK_SET);
1.91      paf       493:                        const char* value;
1.156     moko      494:                        if(reader.read(value, value_size)<value_size)
1.80      paf       495:                                return 0;
1.155     moko      496:                        result=parse_IFD_entry_formatted_value(is_big, format, component_size, components_count, (const uchar*)value);
1.80      paf       497:                }
                    498:                reader.seek(remembered, SEEK_SET);
                    499:        }
                    500: 
                    501:        return result;
                    502: }
                    503: 
1.155     moko      504: static void parse_IFD(HashStringValue& hash, bool is_big, Measure_reader& reader, long tiff_base, bool gps=false);
1.91      paf       505: 
1.155     moko      506: 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       507:        ushort tag=endian_to_ushort(is_big, entry.tag);
1.118     misha     508: 
                    509:        if(tag==JPG_IFD_TAG_EXIF_OFFSET || tag==JPG_IFD_TAG_EXIF_GPS_OFFSET){
1.80      paf       510:                long remembered=reader.tell();
                    511:                {
                    512:                        reader.seek(tiff_base+endian_to_uint(is_big, entry.value_or_offset_to_it), SEEK_SET);
1.118     misha     513:                        parse_IFD(hash, is_big, reader, tiff_base, (tag==JPG_IFD_TAG_EXIF_GPS_OFFSET)?true:gps);
1.80      paf       514:                }
                    515:                reader.seek(remembered, SEEK_SET);
                    516:                return;
                    517:        }
1.118     misha     518: 
1.91      paf       519:        if(Value* value=parse_IFD_entry_value(is_big, reader, tiff_base, entry)) {
1.118     misha     520:                if(const char* name=(gps)?exif_gps_tag_value2name.get(tag):exif_tag_value2name.get(tag))
1.148     moko      521:                        hash.put(name, value);
1.80      paf       522:                else
1.93      paf       523:                        hash.put(String::Body::Format(tag), value);
1.80      paf       524:        }
1.1       paf       525: }
                    526: 
1.155     moko      527: static void parse_IFD(HashStringValue& hash, bool is_big, Measure_reader& reader, long tiff_base, bool gps) {
1.91      paf       528:        const char* buf;
                    529:        if(reader.read(buf, sizeof(JPG_Exif_IFD_begin))<sizeof(JPG_Exif_IFD_begin))
1.80      paf       530:                return;
1.91      paf       531:        JPG_Exif_IFD_begin *start=(JPG_Exif_IFD_begin *)buf;
1.80      paf       532: 
                    533:        ushort directory_entry_count=endian_to_ushort(is_big, start->directory_entry_count);
                    534:        for(int i=0; i<directory_entry_count; i++) {
                    535:                if(reader.read(buf, sizeof(JPG_Exif_IFD_entry))<sizeof(JPG_Exif_IFD_entry))
                    536:                        return;
                    537: 
1.118     misha     538:                parse_IFD_entry(hash, is_big, reader, tiff_base, *(JPG_Exif_IFD_entry *)buf, gps);
1.80      paf       539:        }
                    540:        // then goes: LLLLLLLL Offset to next IFD [not going there]
                    541: }
                    542: 
1.162     moko      543: static Value* parse_exif(Measure_reader& reader) {
1.91      paf       544:        const char* buf;
1.80      paf       545: 
                    546:        uint tiff_base=reader.tell();
                    547:        if(reader.read(buf, sizeof(JPG_Exif_TIFF_header))<sizeof(JPG_Exif_TIFF_header))
                    548:                return 0;
                    549: 
                    550:        JPG_Exif_TIFF_header *head=(JPG_Exif_TIFF_header *)buf;
                    551:        bool is_big=head->byte_align_identifier[0]=='M'; // [M]otorola vs [I]ntel
                    552: 
                    553:        uint first_IFD_offset=endian_to_uint(is_big, head->first_IFD_offset);
                    554:        reader.seek(tiff_base+first_IFD_offset, SEEK_SET);
                    555: 
1.91      paf       556:        VHash* vhash=new VHash;
1.80      paf       557: 
                    558:        // IFD
1.91      paf       559:        parse_IFD(vhash->hash(), is_big, reader, tiff_base);
1.80      paf       560: 
1.91      paf       561:        return vhash;
1.80      paf       562: }
                    563: 
1.163     moko      564: static Value* parse_xmp(Measure_reader& reader, ushort xmp_length, Measure_info &info) {
1.162     moko      565:        const char* buf;
                    566:        if(reader.read(buf, xmp_length)<xmp_length)
                    567:                return 0;
1.163     moko      568: 
                    569:        String::C xmp = Charset::transcode(String::C(pa_strdup(buf, xmp_length), xmp_length), *info.xmp_charset, pa_thread_request().charsets.source());
                    570:        return new VString(*new String(xmp, String::L_TAINTED));
1.162     moko      571: }
                    572: 
                    573: static void measure_jpeg(const String& origin_string, Measure_reader& reader, Measure_info &info) {
1.2       paf       574:        // JFIF format markers
1.80      paf       575:        const uchar MARKER=0xFF;
                    576:        const uchar CODE_SIZE_A=0xC0;
                    577:        const uchar CODE_SIZE_B=0xC1;
                    578:        const uchar CODE_SIZE_C=0xC2;
                    579:        const uchar CODE_SIZE_D=0xC3;
1.162     moko      580:        const uchar CODE_APP1=0xE1;
1.2       paf       581: 
1.91      paf       582:        const char* buf;
1.18      paf       583:        const size_t prefix_size=2;
1.31      parser    584:        if(reader.read(buf, prefix_size)<prefix_size)
1.155     moko      585:                throw Exception(IMAGE_FORMAT, &origin_string, "not JPEG file - too small");
1.80      paf       586:        uchar *signature=(uchar *)buf;
1.1       paf       587:        
1.31      parser    588:        if(!(signature[0]==0xFF && signature[1]==0xD8)) 
1.155     moko      589:                throw Exception(IMAGE_FORMAT, &origin_string, "not JPEG file - wrong signature");
1.31      parser    590: 
                    591:        while(true) {
1.80      paf       592:                uint segment_base=reader.tell()+2/*marker,code*/;
1.31      parser    593:                if(reader.read(buf, sizeof(JPG_Segment_head))<sizeof(JPG_Segment_head))
1.79      paf       594:                        break;
1.31      parser    595:                JPG_Segment_head *head=(JPG_Segment_head *)buf;
                    596: 
1.162     moko      597:                // Verify that it's a valid segment.
1.31      parser    598:                if(head->marker!=MARKER)
1.155     moko      599:                        throw Exception(IMAGE_FORMAT, &origin_string, "not JPEG file - marker not found");
1.79      paf       600: 
1.162     moko      601:                ushort segment_length=endian_to_ushort(true, head->length);
                    602: 
1.79      paf       603:                switch(head->code) {
1.162     moko      604:                // http://dev.exiv2.org/projects/exiv2/wiki/The_Metadata_in_JPEG_files
                    605:                case CODE_APP1:
                    606:                        {
                    607:                                const size_t EXIF_SIG_LEN=6; // Exif\0\0
                    608:                                const size_t XMP_SIG_LEN=29; // http://ns.adobe.com/xap/1.0/\0
                    609: 
                    610:                                if(segment_length<EXIF_SIG_LEN+2 || reader.read(buf, EXIF_SIG_LEN)<EXIF_SIG_LEN)
                    611:                                        break;
                    612:                                if(memcmp(buf, "Exif\0\0", EXIF_SIG_LEN)==0){
                    613:                                        if(info.exif && !*info.exif) // backward compatibility: using first segment
                    614:                                                *info.exif=parse_exif(reader);
                    615:                                        break;
                    616:                                }
                    617: 
                    618:                                if(memcmp(buf, "http:/", EXIF_SIG_LEN))
                    619:                                        break;
                    620:                                if(segment_length<XMP_SIG_LEN+2 || reader.read(buf, XMP_SIG_LEN-EXIF_SIG_LEN)<XMP_SIG_LEN-EXIF_SIG_LEN)
                    621:                                        break;
                    622:                                if(memcmp(buf, "/ns.adobe.com/xap/1.0/\0", XMP_SIG_LEN-EXIF_SIG_LEN)==0){
                    623:                                        if(info.xmp && !*info.xmp) // backward compatibility: using first segment
1.163     moko      624:                                                *info.xmp=parse_xmp(reader, segment_length - XMP_SIG_LEN - 2 /* segment_length */, info);
1.162     moko      625:                                }
                    626: 
                    627:                        }
1.1       paf       628:                        break;
1.31      parser    629: 
1.79      paf       630:                case CODE_SIZE_A:
                    631:                case CODE_SIZE_B:
                    632:                case CODE_SIZE_C:
                    633:                case CODE_SIZE_D:
                    634:                        {
                    635:                                // Segments that contain size info
                    636:                                if(reader.read(buf, sizeof(JPG_Size_segment_body))<sizeof(JPG_Size_segment_body))
1.155     moko      637:                                        throw Exception(IMAGE_FORMAT, &origin_string, "not JPEG file - can not fully read Size segment");
1.79      paf       638:                                JPG_Size_segment_body *body=(JPG_Size_segment_body *)buf;
                    639:                                
1.162     moko      640:                                info.width=endian_to_ushort(true, body->width);
                    641:                                info.height=endian_to_ushort(true, body->height);
                    642:                        }
1.80      paf       643:                        return;
1.79      paf       644:                };
                    645: 
1.162     moko      646:                reader.seek(segment_base + segment_length, SEEK_SET);
1.31      parser    647:        }
                    648: 
1.155     moko      649:        throw Exception(IMAGE_FORMAT, &origin_string, "broken JPEG file - size frame not found");
1.1       paf       650: }
                    651: 
1.169   ! moko      652: ///*********************************************** TIFF
        !           653: 
1.165     moko      654: static bool parse_tiff_IFD(bool is_big, Measure_reader& reader, Measure_info &info) {
                    655:        const char* buf;
                    656:        if(reader.read(buf, sizeof(JPG_Exif_IFD_begin))<sizeof(JPG_Exif_IFD_begin))
                    657:                return false;
                    658:        JPG_Exif_IFD_begin *start=(JPG_Exif_IFD_begin *)buf;
                    659: 
                    660:        ushort directory_entry_count=endian_to_ushort(is_big, start->directory_entry_count);
                    661:        for(int i=0; i<directory_entry_count; i++) {
                    662:                if(reader.read(buf, sizeof(JPG_Exif_IFD_entry))<sizeof(JPG_Exif_IFD_entry))
                    663:                        return false;
                    664: 
                    665:                JPG_Exif_IFD_entry *entry=(JPG_Exif_IFD_entry *)buf;
                    666:                ushort entry_tag=endian_to_ushort(is_big, entry->tag);
                    667: 
                    668:                if(entry_tag == 256 || entry_tag == 257){
                    669:                        ushort entry_format=endian_to_ushort(is_big, entry->format);
                    670:                        if(entry_format != 3 && entry_format != 4 || endian_to_uint(is_big, entry->components_count) != 1)
                    671:                                return false;
                    672:                        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);
                    673:                        (entry_tag == 256) ? info.width=value : info.height=value;
                    674:                        if(info.width && info.height)
                    675:                                return true;
                    676:                }
                    677:        }
                    678: 
                    679:        return false;
                    680:        // then goes: LLLLLLLL Offset to next IFD [not going there]
                    681: }
                    682: 
                    683: static void measure_tiff(const String& origin_string, Measure_reader& reader, Measure_info &info) {
                    684:        const char* buf;
                    685: 
                    686:        if(reader.read(buf, sizeof(JPG_Exif_TIFF_header))<sizeof(JPG_Exif_TIFF_header))
                    687:                throw Exception(IMAGE_FORMAT, &origin_string, "not TIFF file - too small");
                    688: 
                    689:        JPG_Exif_TIFF_header *head=(JPG_Exif_TIFF_header *)buf;
                    690: 
                    691:        if(strncmp(head->byte_align_identifier, "II", 2)!=0 && strncmp(head->byte_align_identifier, "MM", 2)!=0)
                    692:                throw Exception(IMAGE_FORMAT, &origin_string, "not TIFF file - wrong signature");
                    693: 
                    694:        bool is_big=head->byte_align_identifier[0]=='M'; // [M]otorola vs [I]ntel
                    695: 
                    696:        if(endian_to_ushort(is_big, head->signature) != 42)
                    697:                throw Exception(IMAGE_FORMAT, &origin_string, "not TIFF file - wrong signature");
                    698: 
                    699:        reader.seek(endian_to_uint(is_big, head->first_IFD_offset), SEEK_SET);
                    700:        if(!parse_tiff_IFD(is_big, reader, info))
                    701:                throw Exception(IMAGE_FORMAT, &origin_string, "broken TIFF file - size field entry not found");
                    702: }
                    703: 
1.169   ! moko      704: 
        !           705: ///*********************************************** GIF
        !           706: 
        !           707: struct GIF_Header {
        !           708:        char    signature[3];   // 'GIF'
        !           709:        char    version[3];
        !           710:        uchar   width[2];
        !           711:        uchar   height[2];
        !           712:        char    dif;
        !           713:        char    fonColor;
        !           714:        char    nulls;
        !           715: };
        !           716: 
1.164     moko      717: static void measure_gif(const String& origin_string, Measure_reader& reader, ushort& width, ushort& height) {
                    718: 
                    719:        const char* buf;
                    720:        const size_t head_size=sizeof(GIF_Header);
                    721:        if(reader.read(buf, head_size)<head_size)
                    722:                throw Exception(IMAGE_FORMAT, &origin_string, "not GIF file - too small");
                    723:        GIF_Header *head=(GIF_Header *)buf;
                    724: 
                    725:        if(strncmp(head->signature, "GIF", 3)!=0)
                    726:                throw Exception(IMAGE_FORMAT, &origin_string, "not GIF file - wrong signature");
                    727: 
                    728:        width=endian_to_ushort(false, head->width);
                    729:        height=endian_to_ushort(false, head->height);
                    730: }
                    731: 
1.169   ! moko      732: 
        !           733: ///*********************************************** PNG
        !           734: 
        !           735: struct PNG_Header {
        !           736:        char    dummy[12];
        !           737:        char    signature[4];   //< must be "IHDR"
        !           738:        uchar   high_width[2];  //< image width high bytes [we ignore for now]
        !           739:        uchar   width[2];       //< image width low bytes
        !           740:        uchar   high_height[2]; //< image height high bytes [we ignore for now]
        !           741:        uchar   height[4];      //< image height
        !           742: };
        !           743: 
1.152     moko      744: static void measure_png(const String& origin_string, Measure_reader& reader, ushort& width, ushort& height) {
1.72      paf       745: 
1.91      paf       746:        const char* buf;
1.102     paf       747:        const size_t head_size=sizeof(PNG_Header);
                    748:        if(reader.read(buf, head_size)<head_size)
1.152     moko      749:                throw Exception(IMAGE_FORMAT, &origin_string, "not PNG file - too small");
1.72      paf       750:        PNG_Header *head=(PNG_Header *)buf;
                    751: 
                    752:        if(strncmp(head->signature, "IHDR", 4)!=0)
1.152     moko      753:                throw Exception(IMAGE_FORMAT, &origin_string, "not PNG file - wrong signature");
1.72      paf       754: 
1.80      paf       755:        width=endian_to_ushort(true, head->width);
                    756:        height=endian_to_ushort(true, head->height);
1.72      paf       757: }
                    758: 
1.169   ! moko      759: 
        !           760: ///*********************************************** BMP
        !           761: 
        !           762: struct BMP_Header {
        !           763:        char    signature[2];   // 'BM'
        !           764:        uchar   file_size[4];
        !           765:        uchar   reserved[4];
        !           766:        uchar   bitmap_offset[4];
        !           767:        uchar   header_size[4];
        !           768:        uchar   width[2];
        !           769:        uchar   high_width[2];  //< image width high bytes [we ignore for now]
        !           770:        uchar   height[2];
        !           771:        uchar   high_height[2]; //< image height high bytes [we ignore for now]
        !           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.168     moko      793: 
1.169   ! moko      794: ///*********************************************** WEBP
        !           795: 
        !           796: struct WEBP_Header {
        !           797:        char signature_riff[4]; // 'RIFF'
        !           798:        uchar file_size[4];
        !           799:        char signature[4];      // 'WEBP'
        !           800:        char format[4];         // 'VP8 ' or 'VP8L' or 'VP8X'
        !           801: };
        !           802: 
        !           803: struct WEBP_VP8_Chunk {
        !           804:        uchar size[4];
        !           805:        char tag[3];
        !           806:        uchar signature[3];    // 0x9D 0x01 0x2A
        !           807:        uchar width[2];        // 14 bits each
        !           808:        uchar height[2];       // 14 bits each
        !           809: };
        !           810: 
        !           811: struct WEBP_VP8L_Chunk {
        !           812:        uchar size[4];
        !           813:        char signature;        // 0x2F
        !           814:        uchar width_height[4]; // 14 bits each
        !           815: };
        !           816: 
        !           817: struct WEBP_X_Chunk {
        !           818:        uchar size[4];
        !           819:        char reserved[4];
        !           820:        uchar width[3];
        !           821:        uchar height[3];
        !           822: };
        !           823: 
1.168     moko      824: static void measure_webp(const String& origin_string, Measure_reader& reader, ushort& width, ushort& height) {
                    825:        const char* buf;
                    826: 
                    827:        if(reader.read(buf, sizeof(WEBP_Header))<sizeof(WEBP_Header))
                    828:                throw Exception(IMAGE_FORMAT, &origin_string, "not WEBP file - too small");
                    829: 
                    830:        WEBP_Header *head=(WEBP_Header *)buf;
                    831: 
                    832:        if(strncmp(head->signature_riff, "RIFF", 4)!=0 || strncmp(head->signature, "WEBP", 4)!=0)
                    833:                throw Exception(IMAGE_FORMAT, &origin_string, "not WEBP file - wrong signature");
                    834: 
                    835:        if(strncmp(head->format, "VP8 ", 4)==0){
                    836:                if(reader.read(buf, sizeof(WEBP_VP8_Chunk))<sizeof(WEBP_VP8_Chunk))
                    837:                        throw Exception(IMAGE_FORMAT, &origin_string, "broken WEBP file - too small VP8 chunk");
                    838: 
                    839:                WEBP_VP8_Chunk *chunk=(WEBP_VP8_Chunk *)buf;
                    840:                if (chunk->signature[0] != 0x9D || chunk->signature[1] != 0x01 || chunk->signature[2] != 0x2A)
                    841:                        throw Exception(IMAGE_FORMAT, &origin_string, "broken WEBP file - wrong VP8 chunk signature");
                    842: 
                    843:                width=endian_to_ushort(false, chunk->width) & 0x3FFF;
                    844:                height=endian_to_ushort(false, chunk->height) & 0x3FFF;
                    845:        } else if(strncmp(head->format, "VP8L", 4)==0){
                    846:                if(reader.read(buf, sizeof(WEBP_VP8L_Chunk))<sizeof(WEBP_VP8L_Chunk))
                    847:                        throw Exception(IMAGE_FORMAT, &origin_string, "broken WEBP file - too small VP8L chunk");
                    848: 
                    849:                WEBP_VP8L_Chunk *chunk=(WEBP_VP8L_Chunk *)buf;
                    850:                if(chunk->signature != 0x2F)
                    851:                        throw Exception(IMAGE_FORMAT, &origin_string, "broken WEBP file - wrong VP8L chunk signature");
                    852: 
                    853:                uint wh=endian_to_uint(false, chunk->width_height);
                    854:                width=(wh & 0x3FFF) + 1;
                    855:                height=((wh >> 14) & 0x3FFF) + 1;
                    856:        } else if (strncmp(head->format, "VP8X", 4)==0){
                    857:                if(reader.read(buf, sizeof(WEBP_X_Chunk))<sizeof(WEBP_X_Chunk))
                    858:                        throw Exception(IMAGE_FORMAT, &origin_string, "broken WEBP file - too small VP8X chunk");
                    859: 
                    860:                WEBP_X_Chunk *chunk=(WEBP_X_Chunk *)buf;
                    861: 
                    862:                width=endian_to_ushort(false, chunk->width) + 1;   // we ignore third byte to simplify code
                    863:                height=endian_to_ushort(false, chunk->height) + 1; // we ignore third byte to simplify code
                    864:        } else throw Exception(IMAGE_FORMAT, &origin_string, "broken WEBP file - invalid chunk signature");
                    865: }
                    866: 
1.169   ! moko      867: ///*********************************************** measure center
1.1       paf       868: 
1.162     moko      869: static void measure(const String& file_name, Measure_reader& reader, Measure_info &info) {
1.128     misha     870:        const char* file_name_cstr=file_name.taint_cstr(String::L_FILE_SPEC);
1.91      paf       871:        if(const char* cext=strrchr(file_name_cstr, '.')) {
1.1       paf       872:                cext++;
                    873:                if(strcasecmp(cext, "GIF")==0)
1.162     moko      874:                        measure_gif(file_name, reader, info.width, info.height);
1.165     moko      875:                else if(strcasecmp(cext, "JPG")==0 || strcasecmp(cext, "JPEG")==0)
1.162     moko      876:                        measure_jpeg(file_name, reader, info);
1.72      paf       877:                else if(strcasecmp(cext, "PNG")==0)
1.162     moko      878:                        measure_png(file_name, reader, info.width, info.height);
1.164     moko      879:                else if(strcasecmp(cext, "BMP")==0)
                    880:                        measure_bmp(file_name, reader, info.width, info.height);
1.168     moko      881:                else if(strcasecmp(cext, "WEBP")==0)
                    882:                        measure_webp(file_name, reader, info.width, info.height);
1.165     moko      883:                else if(strcasecmp(cext, "TIF")==0 || strcasecmp(cext, "TIFF")==0)
                    884:                        measure_tiff(file_name, reader, info);
1.1       paf       885:                else
1.152     moko      886:                        throw Exception(IMAGE_FORMAT, &file_name, "unhandled image file name extension '%s'", cext);
1.1       paf       887:        } else
1.152     moko      888:                throw Exception(IMAGE_FORMAT, &file_name, "can not determine image type - no file name extension");
1.1       paf       889: }
                    890: 
1.77      paf       891: // methods
1.1       paf       892: 
1.152     moko      893: static void file_measure_action(struct stat& /*finfo*/, int f, const String& file_spec, void *context) {
                    894:        Measure_file_reader reader(f, file_spec);
1.162     moko      895:        measure(file_spec, reader, *static_cast<Measure_info *>(context));
1.1       paf       896: }
                    897: 
1.91      paf       898: static void _measure(Request& r, MethodParams& params) {
                    899:        Value& data=params.as_no_junction(0, "data must not be code");
1.1       paf       900: 
1.91      paf       901:        Value* exif=0;
1.162     moko      902:        Value* xmp=0;
1.163     moko      903:        Measure_info info={ 0, 0, 0, 0, &pa_UTF8_charset };
                    904: 
                    905:        if(params.count()>1)
                    906:                if(HashStringValue* options=params.as_hash(1, "methods options")) {
                    907:                        int valid_options=0;
                    908:                        for(HashStringValue::Iterator i(*options); i; i.next() ){
                    909:                                String::Body key=i.key();
                    910:                                Value* value=i.value();
                    911:                                if(key == "exif") {
                    912:                                        if(r.process(*value).as_bool())
                    913:                                                info.exif=&exif;
                    914:                                        valid_options++;
                    915:                                }
                    916:                                if(key == "xmp") {
                    917:                                        if(r.process(*value).as_bool())
                    918:                                                info.xmp=&xmp;
                    919:                                        valid_options++;
                    920:                                }
                    921:                                if(key == "xmp-charset") {
                    922:                                        info.xmp_charset=&pa_charsets.get(value->as_string());
                    923:                                        valid_options++;
                    924:                                }
                    925:                        }
                    926:                        if(valid_options!=options->count())
                    927:                                throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
                    928:                }
1.162     moko      929: 
1.91      paf       930:        const String* file_name;
1.162     moko      931: 
                    932:        if(file_name=data.get_string()) {
1.167     moko      933:                file_read_action_under_lock(r.full_disk_path(*file_name), "measure", file_measure_action, &info);
1.1       paf       934:        } else {
1.129     misha     935:                VFile* vfile=data.as_vfile(String::L_AS_IS);
1.91      paf       936:                file_name=&vfile->fields().get(name_name)->as_string();
1.162     moko      937:                Measure_buf_reader reader(vfile->value_ptr(), vfile->value_size(), *file_name);
                    938:                measure(*file_name, reader, info);
1.1       paf       939:        }
                    940: 
1.162     moko      941:        GET_SELF(r, VImage).set(file_name, info.width, info.height, 0, exif, xmp);
1.1       paf       942: }
                    943: 
1.161     moko      944: static void append_attrib_pair(String &tag, String::Body key, Value* value){
                    945:        // skip border attribute with empty value
                    946:        if(key=="border" && !value->is_defined())
1.4       paf       947:                return;
                    948: 
1.133     misha     949:        // src="a.gif" width="123" ismap[=-1]
1.161     moko      950:        tag << " " << key;
1.91      paf       951:        if(value->is_string() || value->as_int()>=0)
1.161     moko      952:                tag << "=\"" << value->as_string() << "\"";
1.3       paf       953: }
1.161     moko      954: 
1.91      paf       955: static void _html(Request& r, MethodParams& params) {
                    956:        String tag;
1.3       paf       957:        tag << "<img";
1.4       paf       958: 
1.91      paf       959:        const HashStringValue& fields=GET_SELF(r, VImage).fields();
                    960:        HashStringValue* attribs=0;
1.4       paf       961: 
1.91      paf       962:        if(params.count()) {
1.69      paf       963:                // for backward compatibility: someday was ^html{}
1.154     moko      964:                Value& vattribs=r.process(params[0]);
1.145     moko      965:                if(!vattribs.is_string()) { // allow empty
1.100     paf       966:                        if((attribs=vattribs.get_hash())) {
1.161     moko      967:                                for(HashStringValue::Iterator i(*attribs); i; i.next() )
                    968:                                        append_attrib_pair(tag, i.key(), i.value());
1.39      parser    969:                        } else
1.155     moko      970:                                throw Exception(PARSER_RUNTIME, 0, "attributes must be hash");
1.145     moko      971:                }
1.39      parser    972:        }
1.4       paf       973: 
1.161     moko      974:        for(HashStringValue::Iterator i(fields); i; i.next() ){
                    975:                String::Body key=i.key();
                    976:                // skip user-specified attributes
                    977:                if(attribs && attribs->get(key))
                    978:                        continue;
                    979:                // allow only html attributes (to exclude exif, line-*)
                    980:                if(key=="src" || key=="width" || key=="height" || key=="border")
                    981:                        append_attrib_pair(tag, key, i.value());
1.91      paf       982:        }
1.161     moko      983: 
1.6       paf       984:        tag << " />";
1.157     moko      985:        r.write(tag);
1.3       paf       986: }
1.8       paf       987: 
1.68      paf       988: /// @test wrap FILE to auto-object
1.155     moko      989: static gdImage* load(Request& r, const String& file_name){
1.167     moko      990:        const char* file_name_cstr=r.full_disk_path(file_name).taint_cstr(String::L_FILE_SPEC);
1.159     moko      991:        if(FILE *f=pa_fopen(file_name_cstr, "rb")) {
1.91      paf       992:                gdImage* image=new gdImage;
                    993:                bool ok=image->CreateFromGif(f);
1.16      paf       994:                fclose(f);
1.23      paf       995:                if(!ok)
1.155     moko      996:                        throw Exception(IMAGE_FORMAT, &file_name, "is not in GIF format");
1.91      paf       997:                return image;
1.16      paf       998:        } else {
1.155     moko      999:                throw Exception("file.missing", 0, "can not open '%s'", file_name_cstr);
1.16      paf      1000:        }
                   1001: }
                   1002: 
                   1003: 
1.91      paf      1004: static void _load(Request& r, MethodParams& params) {
1.116     misha    1005:        const String& file_name=params.as_string(0, FILE_NAME_MUST_NOT_BE_CODE);
1.6       paf      1006: 
1.91      paf      1007:        gdImage* image=load(r, file_name);
                   1008:        GET_SELF(r, VImage).set(&file_name, image->SX(), image->SY(), image);
1.6       paf      1009: }
                   1010: 
1.91      paf      1011: static void _create(Request& r, MethodParams& params) {
                   1012:        int width=params.as_int(0, "width must be int", r);
                   1013:        int height=params.as_int(1, "height must be int", r);
1.8       paf      1014:        int bgcolor_value=0xffFFff;
1.91      paf      1015:        if(params.count()>2)
                   1016:                bgcolor_value=params.as_int(2, "color must be int", r);
                   1017:        gdImage* image=new gdImage;
                   1018:        image->Create(width, height);
                   1019:        image->FilledRectangle(0, 0, width-1, height-1, image->Color(bgcolor_value));
                   1020:        GET_SELF(r, VImage).set(0, width, height, image);
1.6       paf      1021: }
                   1022: 
1.91      paf      1023: static void _gif(Request& r, MethodParams& params) {
1.108     paf      1024:        gdImage& image=GET_SELF(r, VImage).image();
1.6       paf      1025: 
1.138     misha    1026:        const String *file_name=params.count()>0?&params.as_string(0, FILE_NAME_MUST_BE_STRING):0;
1.10      paf      1027: 
1.108     paf      1028:        gdBuf buf=image.Gif();
1.6       paf      1029:        
1.91      paf      1030:        VFile& vfile=*new VFile;
1.137     misha    1031: 
1.142     moko     1032:        vfile.set_binary(false/*not tainted*/, (const char *)buf.ptr, buf.size, file_name, new VString(*new String("image/gif")));
1.137     misha    1033: 
1.157     moko     1034:        r.write(vfile);
1.6       paf      1035: }
                   1036: 
1.91      paf      1037: static void _line(Request& r, MethodParams& params) {
1.108     paf      1038:        gdImage& image=GET_SELF(r, VImage).image();
1.12      paf      1039: 
1.108     paf      1040:        image.Line(
1.91      paf      1041:                params.as_int(0, "x0 must be int", r), 
                   1042:                params.as_int(1, "y0 must be int", r), 
                   1043:                params.as_int(2, "x1 must be int", r), 
                   1044:                params.as_int(3, "y1 must be int", r), 
1.108     paf      1045:                image.Color(params.as_int(4, "color must be int", r)));
1.13      paf      1046: }
                   1047: 
1.91      paf      1048: static void _fill(Request& r, MethodParams& params) {
1.108     paf      1049:        gdImage& image=GET_SELF(r, VImage).image();
1.12      paf      1050: 
1.108     paf      1051:        image.Fill(
1.91      paf      1052:                params.as_int(0, "x must be int", r), 
                   1053:                params.as_int(1, "y must be int", r), 
1.108     paf      1054:                image.Color(params.as_int(2, "color must be int", r)));
1.13      paf      1055: }
                   1056: 
1.91      paf      1057: static void _rectangle(Request& r, MethodParams& params) {
1.108     paf      1058:        gdImage& image=GET_SELF(r, VImage).image();
1.13      paf      1059: 
1.108     paf      1060:        image.Rectangle(
1.91      paf      1061:                params.as_int(0, "x0 must be int", r), 
                   1062:                params.as_int(1, "y0 must be int", r), 
                   1063:                params.as_int(2, "x1 must be int", r), 
                   1064:                params.as_int(3, "y1 must be int", r), 
1.108     paf      1065:                image.Color(params.as_int(4, "color must be int", r)));
1.13      paf      1066: }
                   1067: 
1.91      paf      1068: static void _bar(Request& r, MethodParams& params) {
1.108     paf      1069:        gdImage& image=GET_SELF(r, VImage).image();
1.13      paf      1070: 
1.108     paf      1071:        image.FilledRectangle(
1.91      paf      1072:                params.as_int(0, "x0 must be int", r), 
                   1073:                params.as_int(1, "y0 must be int", r), 
                   1074:                params.as_int(2, "x1 must be int", r), 
                   1075:                params.as_int(3, "y1 must be int", r), 
1.108     paf      1076:                image.Color(params.as_int(4, "color must be int", r)));
1.13      paf      1077: }
                   1078: 
1.44      parser   1079: #ifndef DOXYGEN
1.155     moko     1080: static void add_point(Table::element_type row, gdImage::Point **p) {
1.110     paf      1081:        if(row->count()!=2)
1.155     moko     1082:                throw Exception(0, 0, "coordinates table must contain two columns: x and y values");
1.91      paf      1083:        (**p).x=row->get(0)->as_int();
                   1084:        (**p).y=row->get(1)->as_int();
1.44      parser   1085:        (*p)++;
                   1086: }
                   1087: #endif
1.135     misha    1088: #ifndef DOXYGEN
1.155     moko     1089: static void add_point(int x, int y, gdImage::Point **p) {
1.135     misha    1090:        (**p).x=x;
                   1091:        (**p).y=y;
                   1092:        (*p)++;
                   1093: }
                   1094: #endif
1.91      paf      1095: static void _replace(Request& r, MethodParams& params) {
1.135     misha    1096:        int src_color=params.as_int(0, "src color must be int", r);
                   1097:        int dest_color=params.as_int(1, "dest color must be int", r);
                   1098: 
1.108     paf      1099:        gdImage& image=GET_SELF(r, VImage).image();
1.13      paf      1100: 
1.135     misha    1101:        gdImage::Point* all_p=0;
                   1102:        size_t count=0;
                   1103:        if(params.count() == 3){
1.140     misha    1104:                Table* table=params.as_table(2, "coordinates");
1.135     misha    1105:                count=table->count();
                   1106:                all_p=new(PointerFreeGC) gdImage::Point[count];
                   1107:                gdImage::Point* add_p=all_p;
                   1108:                table->for_each(add_point, &add_p);
                   1109:        } else {
                   1110:                int max_x=image.SX()-1;
                   1111:                int max_y=image.SY()-1;
                   1112:                if(max_x > 0 && max_y > 0){
                   1113:                        count=4;
                   1114:                        all_p=new(PointerFreeGC) gdImage::Point[count];
                   1115:                        gdImage::Point* add_p=all_p;
                   1116:                        add_point(0, 0, &add_p);
                   1117:                        add_point(max_x, 0, &add_p);
                   1118:                        add_point(max_x, max_y, &add_p);
                   1119:                        add_point(0, max_y, &add_p);
                   1120:                }
                   1121:        }
1.13      paf      1122: 
1.135     misha    1123:        if(count)
                   1124:                image.FilledPolygonReplaceColor(all_p, count, image.Color(src_color), image.Color(dest_color));
1.13      paf      1125: }
                   1126: 
1.91      paf      1127: static void _polyline(Request& r, MethodParams& params) {
1.108     paf      1128:        gdImage& image=GET_SELF(r, VImage).image();
1.13      paf      1129: 
1.140     misha    1130:        Table* table=params.as_table(1, "coordinates");
1.44      parser   1131: 
1.92      paf      1132:        gdImage::Point* all_p=new(PointerFreeGC) gdImage::Point[table->count()];
1.44      parser   1133:        gdImage::Point *add_p=all_p;    
                   1134:        table->for_each(add_point, &add_p);
1.108     paf      1135:        image.Polygon(all_p, table->count(), 
                   1136:                image.Color(params.as_int(0, "color must be int", r)),
1.44      parser   1137:                false/*not closed*/);
                   1138: }
                   1139: 
1.91      paf      1140: static void _polygon(Request& r, MethodParams& params) {
1.108     paf      1141:        gdImage& image=GET_SELF(r, VImage).image();
1.13      paf      1142: 
1.140     misha    1143:        Table* table=(Table*)params.as_table(1, "coordinates");
1.44      parser   1144: 
1.92      paf      1145:        gdImage::Point* all_p=new(PointerFreeGC) gdImage::Point[table->count()];
1.44      parser   1146:        gdImage::Point *add_p=all_p;    
                   1147:        table->for_each(add_point, &add_p);
1.108     paf      1148:        image.Polygon(all_p, table->count(), 
                   1149:                image.Color(params.as_int(0, "color must be int", r)));
1.13      paf      1150: }
                   1151: 
1.91      paf      1152: static void _polybar(Request& r, MethodParams& params) {
1.108     paf      1153:        gdImage& image=GET_SELF(r, VImage).image();
1.13      paf      1154: 
1.140     misha    1155:        Table* table=(Table*)params.as_table(1, "coordinates");
1.13      paf      1156: 
1.92      paf      1157:        gdImage::Point* all_p=new(PointerFreeGC) gdImage::Point[table->count()];
1.44      parser   1158:        gdImage::Point *add_p=all_p;    
                   1159:        table->for_each(add_point, &add_p);
1.108     paf      1160:        image.FilledPolygon(all_p, table->count(), 
                   1161:                image.Color(params.as_int(0, "color must be int", r)));
1.12      paf      1162: }
                   1163: 
1.16      paf      1164: // font
                   1165: 
1.85      paf      1166: #define Y(y)(y+index*height)
1.21      paf      1167: 
1.91      paf      1168: // Font class
                   1169: 
1.155     moko     1170: Font::Font(Charset& asource_charset, const String& aalphabet, gdImage* aifont, int aheight, int amonospace, int aspacebarspace, int aletterspacing):
1.158     moko     1171:        letterspacing(aletterspacing),
1.132     misha    1172:        height(aheight),
                   1173:        monospace(amonospace),
                   1174:        spacebarspace(aspacebarspace),
1.100     paf      1175:        ifont(aifont),
1.158     moko     1176:        alphabet(aalphabet),
                   1177:        fsource_charset(asource_charset){
1.132     misha    1178: 
                   1179:        if(fsource_charset.isUTF8()){
                   1180:                size_t index=0;
                   1181:                for(UTF8_string_iterator i(alphabet); i.has_next(); )
                   1182:                        fletter2index.put_dont_replace(i.next(), index++);
                   1183:        }
1.91      paf      1184: }
                   1185: 
                   1186: /* ******************************** char ********************************** */
                   1187: 
                   1188: size_t Font::index_of(char ch) {
                   1189:        if(ch==' ') return STRING_NOT_FOUND;
                   1190:        return alphabet.pos(ch);
                   1191: }
                   1192: 
1.132     misha    1193: size_t Font::index_of(XMLCh ch) {
                   1194:        if(ch==' ') return STRING_NOT_FOUND;
                   1195:        return fletter2index.get(ch);
                   1196: }
                   1197: 
1.91      paf      1198: int Font::index_width(size_t index) {
                   1199:        if(index==STRING_NOT_FOUND)
                   1200:                return spacebarspace;
                   1201:        int tr=ifont->GetTransparent();
                   1202:        for(int x=ifont->SX()-1; x>=0; x--) {
                   1203:                for(int y=0; y<height; y++)
                   1204:                        if(ifont->GetPixel(x, Y(y))!=tr) 
                   1205:                                return x+1;
1.16      paf      1206:        }
1.91      paf      1207:        return 0;
                   1208: }
                   1209: 
1.108     paf      1210: void Font::index_display(gdImage& image, int x, int y, size_t index){
1.91      paf      1211:        if(index!=STRING_NOT_FOUND) 
1.108     paf      1212:                ifont->Copy(image, x, y, 0, Y(0), index_width(index), height);
1.91      paf      1213: }
                   1214: 
                   1215: /* ******************************** string ********************************** */
                   1216: 
                   1217: int Font::step_width(int index) {
1.125     misha    1218:        return letterspacing + (monospace ? monospace : index_width(index));
1.91      paf      1219: }
                   1220: 
                   1221: // counts trailing letter_spacing, consider this OK. useful for contiuations
                   1222: int Font::string_width(const String& s){
                   1223:        const char* cstr=s.cstr();
                   1224:        int result=0;
1.132     misha    1225: 
                   1226:        if(fsource_charset.isUTF8()){
                   1227:                for(UTF8_string_iterator i(s); i.has_next(); )
                   1228:                        result+=step_width(index_of(i.next()));
                   1229:        } else {
                   1230:                for(const char* current=cstr; *current; current++)
                   1231:                        result+=step_width(index_of(*current));
                   1232:        }
                   1233: 
1.91      paf      1234:        return result;
                   1235: }
                   1236: 
1.108     paf      1237: void Font::string_display(gdImage& image, int x, int y, const String& s){
1.91      paf      1238:        const char* cstr=s.cstr();
1.132     misha    1239: 
                   1240:        if(fsource_charset.isUTF8()){
                   1241:                for(UTF8_string_iterator i(s); i.has_next(); ){
                   1242:                        size_t index=index_of(i.next());
                   1243:                        index_display(image, x, y, index);
                   1244:                        x+=step_width(index);
                   1245:                }
                   1246:        } else {
                   1247:                for(const char* current=cstr; *current; current++) {
                   1248:                        size_t index=index_of(*current);
                   1249:                        index_display(image, x, y, index);
                   1250:                        x+=step_width(index);
                   1251:                }
1.87      paf      1252:        }
1.91      paf      1253: }
1.87      paf      1254: 
1.91      paf      1255: //
1.35      parser   1256: 
1.16      paf      1257: 
1.91      paf      1258: static void _font(Request& r, MethodParams& params) {
                   1259:        const String& alphabet=params.as_string(0, "alphabet must not be code");
1.131     misha    1260:        size_t alphabet_length=alphabet.length(r.charsets.source());
                   1261:        if(!alphabet_length)
1.155     moko     1262:                throw Exception(PARSER_RUNTIME, 0, "alphabet must not be empty");
1.84      paf      1263: 
1.125     misha    1264:        gdImage* image=load(r, params.as_string(1, FILE_NAME_MUST_NOT_BE_CODE));
                   1265: 
1.126     misha    1266:        int spacebar_width=image->SX();
1.125     misha    1267:        int monospace_width=0; // proportional
                   1268:        int letter_spacing=1;
                   1269:        if(params.count()>2){
1.144     moko     1270:                if(HashStringValue* options=params[2].get_hash()){
1.125     misha    1271:                        // third option is hash
                   1272:                        if(params.count()>3)
1.144     moko     1273:                                throw Exception(PARSER_RUNTIME, 0, "too many params were specified");
1.125     misha    1274:                        int valid_options=0;
                   1275:                        if(Value* vspacebar_width=options->get(spacebar_width_name)){
                   1276:                                valid_options++;
1.153     moko     1277:                                spacebar_width=r.process(*vspacebar_width).as_int();
1.125     misha    1278:                        }
                   1279:                        if(Value* vmonospace_width=options->get(monospace_width_name)){
                   1280:                                valid_options++;
1.153     moko     1281:                                monospace_width=r.process(*vmonospace_width).as_int();
1.125     misha    1282:                                if(!monospace_width)
                   1283:                                        monospace_width=image->SX();
                   1284:                        }
                   1285:                        if(Value* vletter_spacing=options->get(letter_spacing_name)){
                   1286:                                valid_options++;
1.153     moko     1287:                                letter_spacing=r.process(*vletter_spacing).as_int();
1.125     misha    1288:                        }
                   1289:                        if(valid_options!=options->count())
1.134     misha    1290:                                throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.125     misha    1291:                } else {
                   1292:                        // backward
1.144     moko     1293:                        spacebar_width=params.as_int(2, "param must be int or hash", r);
1.125     misha    1294:                        if(params.count()>3) {
                   1295:                                monospace_width=params.as_int(3, "monospace_width must be int", r);
                   1296:                                if(!monospace_width)
                   1297:                                        monospace_width=image->SX();
                   1298:                        }
                   1299:                }
                   1300:        }
                   1301: 
1.131     misha    1302:        if(int remainder=image->SY() % alphabet_length)
1.155     moko     1303:                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   1304:        
1.155     moko     1305:        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      1306: }
                   1307: 
1.91      paf      1308: static void _text(Request& r, MethodParams& params) {
                   1309:        int x=params.as_int(0, "x must be int", r);
                   1310:        int y=params.as_int(1, "y must be int", r);
                   1311:        const String& s=params.as_string(2, "text must not be code");
1.16      paf      1312: 
1.91      paf      1313:        VImage& vimage=GET_SELF(r, VImage);
1.108     paf      1314:        vimage.font().string_display(vimage.image(), x, y, s);
1.16      paf      1315: }
                   1316: 
1.91      paf      1317: static void _length(Request& r, MethodParams& params) {
                   1318:        const String& s=params.as_string(0, "text must not be code");
1.47      parser   1319: 
1.91      paf      1320:        VImage& vimage=GET_SELF(r, VImage);
1.157     moko     1321:        r.write(*new VInt(vimage.font().string_width(s)));
1.47      parser   1322: }
                   1323: 
1.91      paf      1324: static void _arc(Request& r, MethodParams& params) {
1.108     paf      1325:        gdImage& image=GET_SELF(r, VImage).image();
1.48      parser   1326: 
1.108     paf      1327:        image.Arc(
1.91      paf      1328:                params.as_int(0, "center_x must be int", r), 
                   1329:                params.as_int(1, "center_y must be int", r), 
                   1330:                params.as_int(2, "width must be int", r), 
                   1331:                params.as_int(3, "height must be int", r), 
                   1332:                params.as_int(4, "start degrees must be int", r), 
                   1333:                params.as_int(5, "end degrees must be int", r), 
1.108     paf      1334:                image.Color(params.as_int(6, "cx must be int", r)));
1.48      parser   1335: }
                   1336: 
1.91      paf      1337: static void _sector(Request& r, MethodParams& params) {
1.108     paf      1338:        gdImage& image=GET_SELF(r, VImage).image();
1.49      parser   1339: 
1.108     paf      1340:        image.Sector(
1.91      paf      1341:                params.as_int(0, "center_x must be int", r), 
                   1342:                params.as_int(1, "center_y must be int", r), 
                   1343:                params.as_int(2, "width must be int", r), 
                   1344:                params.as_int(3, "height must be int", r), 
                   1345:                params.as_int(4, "start degrees must be int", r), 
                   1346:                params.as_int(5, "end degrees must be int", r), 
1.108     paf      1347:                image.Color(params.as_int(6, "color must be int", r)));
1.49      parser   1348: }
                   1349: 
1.91      paf      1350: static void _circle(Request& r, MethodParams& params) {
1.108     paf      1351:        gdImage& image=GET_SELF(r, VImage).image();
1.48      parser   1352: 
1.91      paf      1353:        int size=params.as_int(2, "radius must be int", r)*2;
1.108     paf      1354:        image.Arc(
1.91      paf      1355:                params.as_int(0, "center_x must be int", r), 
                   1356:                params.as_int(1, "center_y must be int", r), 
1.50      parser   1357:                size, //w
                   1358:                size, //h
1.48      parser   1359:                0, //s
                   1360:                360, //e
1.108     paf      1361:                image.Color(params.as_int(3, "color must be int", r)));
1.48      parser   1362: }
                   1363: 
1.108     paf      1364: gdImage& as_image(MethodParams& params, int index, const char* msg) {
1.91      paf      1365:        Value& value=params.as_no_junction(index, msg);
1.53      parser   1366: 
1.130     misha    1367:        if(Value* vimage=value.as(VIMAGE_TYPE)) {
1.108     paf      1368:                return static_cast<VImage *>(vimage)->image();
1.75      paf      1369:        } else
1.155     moko     1370:                throw Exception(PARSER_RUNTIME, 0, msg);
1.53      parser   1371: }
                   1372: 
1.91      paf      1373: static void _copy(Request& r, MethodParams& params) {
1.108     paf      1374:        gdImage& dest=GET_SELF(r, VImage).image();
1.53      parser   1375: 
1.108     paf      1376:        gdImage& src=as_image(params, 0, "src must be image");
1.53      parser   1377: 
1.91      paf      1378:        int sx=params.as_int(1, "src_x must be int", r);
                   1379:        int sy=params.as_int(2, "src_y must be int", r);
                   1380:        int sw=params.as_int(3, "src_w must be int", r);
                   1381:        int sh=params.as_int(4, "src_h must be int", r);
                   1382:        int dx=params.as_int(5, "dest_x must be int", r);
                   1383:        int dy=params.as_int(6, "dest_y must be int", r);
                   1384:        if(params.count()>1+2+2+2) {
                   1385:                int dw=params.as_int(1+2+2+2, "dest_w must be int", r);
                   1386:                int dh=(int)(params.count()>1+2+2+2+1?
                   1387:                        params.as_int(1+2+2+2+1, "dest_h must be int", r):sh*(((double)dw)/((double)sw)));
                   1388:                int tolerance=params.count()>1+2+2+2+2?
                   1389:                        params.as_int(1+2+2+2+2, "tolerance must be int", r):150;
1.53      parser   1390: 
1.108     paf      1391:                src.CopyResampled(dest, dx, dy, sx, sy, dw, dh, sw, sh, tolerance);
1.53      parser   1392:        } else
1.108     paf      1393:                src.Copy(dest, dx, dy, sx, sy, sw, sh);
                   1394: }
                   1395: 
                   1396: static void _pixel(Request& r, MethodParams& params) {
                   1397:        gdImage& image=GET_SELF(r, VImage).image();
                   1398: 
1.109     paf      1399:        int x=params.as_int(0, "x must be int", r); 
                   1400:        int y=params.as_int(1, "y must be int", r);
                   1401: 
                   1402:        if(params.count()>2) {
                   1403:                image.SetPixel(x, y, 
                   1404:                        image.Color(params.as_int(2, "color must be int", r)));
                   1405:        } else 
1.157     moko     1406:                r.write(*new VInt(image.DecodeColor(image.GetPixel(x, y))));
1.53      parser   1407: }
                   1408: 
                   1409: 
1.22      paf      1410: // constructor
                   1411: 
1.91      paf      1412: MImage::MImage(): Methoded("image") {
1.1       paf      1413:        // ^image:measure[DATA]
1.163     moko     1414:        // ^image:measure[DATA; $.exif(false) $.xmp(false) $.xmp-charset[UTF-8] ]
                   1415:        add_native_method("measure", Method::CT_DYNAMIC, _measure, 1, 2);
1.3       paf      1416: 
1.25      paf      1417:        // ^image.html[]
                   1418:        // ^image.html[hash]
1.22      paf      1419:        add_native_method("html", Method::CT_DYNAMIC, _html, 0, 1);
1.6       paf      1420: 
1.25      paf      1421:        // ^image.load[background.gif]
1.22      paf      1422:        add_native_method("load", Method::CT_DYNAMIC, _load, 1, 1);
1.6       paf      1423: 
1.25      paf      1424:        // ^image.create[width;height] bgcolor=white
                   1425:        // ^image.create[width;height;bgcolor]
1.22      paf      1426:        add_native_method("create", Method::CT_DYNAMIC, _create, 2, 3);
1.6       paf      1427: 
1.25      paf      1428:        // ^image.gif[]
1.95      paf      1429:        add_native_method("gif", Method::CT_DYNAMIC, _gif, 0, 1);
1.12      paf      1430: 
1.25      paf      1431:        // ^image.line(x0;y0;x1;y1;color)
1.22      paf      1432:        add_native_method("line", Method::CT_DYNAMIC, _line, 5, 5);
1.13      paf      1433: 
1.25      paf      1434:        // ^image.fill(x;y;color)
1.22      paf      1435:        add_native_method("fill", Method::CT_DYNAMIC, _fill, 3, 3);
1.13      paf      1436: 
1.25      paf      1437:        // ^image.rectangle(x0;y0;x1;y1;color)
1.22      paf      1438:        add_native_method("rectangle", Method::CT_DYNAMIC, _rectangle, 5, 5);
1.13      paf      1439: 
1.25      paf      1440:        // ^image.bar(x0;y0;x1;y1;color)
1.22      paf      1441:        add_native_method("bar", Method::CT_DYNAMIC, _bar, 5, 5);
1.13      paf      1442: 
1.44      parser   1443:        // ^image.replace(color-source;color-dest)[table x:y]
1.135     misha    1444:        // ^image.replace(color-source;color-dest)
                   1445:        add_native_method("replace", Method::CT_DYNAMIC, _replace, 2, 3);
1.44      parser   1446: 
                   1447:        // ^image.polyline(color)[table x:y]
                   1448:        add_native_method("polyline", Method::CT_DYNAMIC, _polyline, 2, 2);
1.13      paf      1449: 
1.44      parser   1450:        // ^image.polygon(color)[table x:y]
                   1451:        add_native_method("polygon", Method::CT_DYNAMIC, _polygon, 2, 2);
1.13      paf      1452: 
1.44      parser   1453:        // ^image.polybar(color)[table x:y]
                   1454:        add_native_method("polybar", Method::CT_DYNAMIC, _polybar, 2, 2);
1.13      paf      1455: 
1.125     misha    1456:        // ^image.font[alPHAbet;font-file-name.gif]
                   1457:        // ^image.font[alPHAbet;font-file-name.gif](spacebar_width)
                   1458:        // ^image.font[alPHAbet;font-file-name.gif](spacebar_width;letter_width)
                   1459:        // ^image.font[alPHAbet;font-file-name.gif][$.space-width(.) $.letter-width(.) $.letter-space(.)]
                   1460:        add_native_method("font", Method::CT_DYNAMIC, _font, 2, 4);
1.16      paf      1461: 
1.125     misha    1462:        // ^image.text(x;y)[text]
1.22      paf      1463:        add_native_method("text", Method::CT_DYNAMIC, _text, 3, 3);
1.47      parser   1464:        
1.125     misha    1465:        // ^image.length[text]
1.47      parser   1466:        add_native_method("length", Method::CT_DYNAMIC, _length, 1, 1);
1.16      paf      1467:        
1.48      parser   1468:        // ^image.arc(center x;center y;width;height;start in degrees;end in degrees;color)
                   1469:        add_native_method("arc", Method::CT_DYNAMIC, _arc, 7, 7);
1.49      parser   1470: 
                   1471:        // ^image.sector(center x;center y;width;height;start in degrees;end in degrees;color)
                   1472:        add_native_method("sector", Method::CT_DYNAMIC, _sector, 7, 7);
1.48      parser   1473: 
                   1474:        // ^image.circle(center x;center y;r;color)
                   1475:        add_native_method("circle", Method::CT_DYNAMIC, _circle, 4, 4);
                   1476: 
1.56      parser   1477:        // ^image.copy[source](src x;src y;src w;src h;dst x;dst y[;dest w[;dest h[;tolerance]]])
                   1478:        add_native_method("copy", Method::CT_DYNAMIC, _copy, 1+2+2+2, (1+2+2+2)+2+1);
1.108     paf      1479: 
                   1480:        // ^image.pixel(x;y)[(color)]
1.109     paf      1481:        add_native_method("pixel", Method::CT_DYNAMIC, _pixel, 2, 3);
1.1       paf      1482: }

E-mail: