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

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

E-mail: