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

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

E-mail: