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

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/25 09:34:52 $";
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.15.2.1  (paf       36:):      Value* create_new_value() { return 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.15.2.8  (paf       53:: class EXIF_tag_value2name: public Hash<int, const char*> {
1.90.2.7  paf        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.15.2.5  (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): 
1.90.2.15.2.8  (paf      171::               file_name(afile_name), fname(afname), f(af) {
1.1       paf       172:        }
                    173: 
1.90.2.15.2.5  (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(PointerFreeGC) 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.15.2.1  (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.15.2.1  (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.15.2.5  (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: 
1.90.2.15.2.3  (paf      226::                       throw Exception(0, 
                    227::                               0, 
1.90.2.7  paf       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.15.2.1  (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
1.90.2.15.2.7  (paf      282:: struct JPG_Exif_segment_begin {
1.79      paf       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
1.90.2.15.2.7  (paf      294:: struct JPG_Exif_IFD_begin {
1.80      paf       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_gif(const String& origin_string, 
1.78      paf       331:                         Measure_reader& reader, ushort& width, ushort& height) {
1.1       paf       332: 
1.90.2.15.2.5  (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.90.2.15.2.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.90.2.15.2.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.8  (paf      350:: static Value* 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.15.2.1  (paf      357:):              return new VInt((uchar)value[0]);
1.80      paf       358:        case 3: // unsigned short
1.90.2.15.2.1  (paf      359:):              return 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.15.2.1  (paf      362:):              return 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.15.2.8  (paf      368::                               return 0;
1.90.2.15.2.1  (paf      369:):                      return new VDouble(((double)numerator)/denominator);
1.80      paf       370:                }
                    371:        case 6: // signed byte
1.90.2.15.2.1  (paf      372:):              return new VInt((signed char)value[0]);
1.80      paf       373:        case 8: // signed short
1.90.2.15.2.1  (paf      374:):              return new VInt((signed short)endian_to_ushort(is_big, value));
1.80      paf       375:        case 9: // signed long
1.90.2.15.2.1  (paf      376:):              return 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.15.2.8  (paf      382::                               return 0;
1.90.2.15.2.1  (paf      383:):                      return 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.15.2.8  (paf      393::       return 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, bool fail_on_error);
1.89      paf       398: 
1.90.2.15.2.8  (paf      399:: static Value* 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.15.2.1  (paf      416:):                              return 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 new VString(*new String(cstr, size, true/*tainted*/));
1.80      paf       422:        }
                    423: 
                    424:        if(components_count==1)
1.90.2.15.2.4  (paf      425::               return parse_IFD_entry_formatted_one_value(is_big, format, component_size, value);
1.80      paf       426: 
1.90.2.15.2.1  (paf      427:):      VHash* result=new VHash;
                    428:):      HashStringValue& hash=result->hash();
1.80      paf       429:        for(uint i=0; i<components_count; i++, value+=component_size) {
1.90.2.15.2.1  (paf      430:):              hash.put(
                    431:):                      StringBody::Format(i),
1.90.2.15.2.4  (paf      432::                       parse_IFD_entry_formatted_one_value(is_big, format, component_size, value));
1.80      paf       433:        }
                    434: 
1.90.2.7  paf       435:        return result;
1.80      paf       436: }
                    437: 
1.90.2.15.2.8  (paf      438:: static Value* parse_IFD_entry_value(
1.80      paf       439:                                                                        bool is_big, Measure_reader& reader, long tiff_base,
                    440:                                                                        JPG_Exif_IFD_entry& entry) {
                    441:        size_t format2component_size[]={
                    442:                0, // undefined
                    443:                1, // unsigned byte
                    444:                1, // ascii string
                    445:                2, // unsigned short
                    446:                4, // unsigned long
                    447:                8, // unsigned rational
                    448:                1, // signed byte
                    449:                0, // undefined
                    450:                2, // signed short
                    451:                4, // signed long
                    452:                8, // signed rational
                    453:                /*
                    454:                4, // single float
                    455:                8, // double float
                    456:                */
                    457:        };
                    458: 
                    459:        ushort format=endian_to_ushort(is_big, entry.format);
                    460:        if(format>=sizeof(format2component_size)/sizeof(format2component_size[0]))
1.90.2.15.2.8  (paf      461::               return 0; // format out of range, ignoring
1.80      paf       462: 
                    463:        size_t component_size=format2component_size[format];
                    464:        if(component_size==0)
1.90.2.15.2.8  (paf      465::               return 0; // undefined format
1.80      paf       466: 
                    467:        // You can get the total data byte length by multiplies 
                    468:        // a 'bytes/components' value (see above chart) by number of components stored 'NNNNNNNN' area
                    469:        uint components_count=endian_to_uint(is_big, entry.components_count);
                    470:        size_t value_size=component_size*components_count;
                    471:        // If its size is over 4bytes, 'DDDDDDDD' contains the offset to data stored address
1.90.2.15.2.8  (paf      472::       Value* result;
1.80      paf       473: 
                    474:        if(value_size<=4)
1.90.2.15.2.1  (paf      475:):              result=parse_IFD_entry_formatted_value(
1.80      paf       476:                        is_big, format, 
                    477:                        component_size, components_count, 
                    478:                        entry.value_or_offset_to_it);
                    479:        else {
                    480:                long remembered=reader.tell();
                    481:                {
                    482:                        reader.seek(tiff_base+endian_to_uint(is_big, entry.value_or_offset_to_it), SEEK_SET);
1.90.2.15.2.5  (paf      483::                       const char* value;
1.80      paf       484:                        if(reader.read(value, value_size)<sizeof(value_size))
1.90.2.15.2.8  (paf      485::                               return 0;
1.90.2.15.2.1  (paf      486:):                      result=parse_IFD_entry_formatted_value(
1.80      paf       487:                                is_big, format, 
                    488:                                component_size, components_count, 
                    489:                                (const uchar*)value);
                    490:                }
                    491:                reader.seek(remembered, SEEK_SET);
                    492:        }
                    493: 
                    494:        return result;
                    495: }
                    496: 
1.90.2.15.2.1  (paf      497:): static void parse_IFD(HashStringValue& hash,
                    498:):                    bool is_big, Measure_reader& reader, long tiff_base);
1.80      paf       499: 
1.90.2.15.2.1  (paf      500:): static void parse_IFD_entry(HashStringValue& hash,
                    501:):                          bool is_big, Measure_reader& reader, long tiff_base,
                    502:):                          JPG_Exif_IFD_entry& entry) {
1.80      paf       503:        ushort tag=endian_to_ushort(is_big, entry.tag);
                    504:        if(tag==JPG_IFD_TAG_EXIF_OFFSET) {
                    505:                long remembered=reader.tell();
                    506:                {
                    507:                        reader.seek(tiff_base+endian_to_uint(is_big, entry.value_or_offset_to_it), SEEK_SET);
1.90.2.15.2.4  (paf      508::                       parse_IFD(hash, is_big, reader, tiff_base);
1.80      paf       509:                }
                    510:                reader.seek(remembered, SEEK_SET);
                    511:                return;
                    512:        }
                    513:        
1.90.2.15.2.8  (paf      514::       if(Value* value=parse_IFD_entry_value(is_big, reader, tiff_base, entry)) {
1.90.2.13  paf       515:                if(const char* name=exif_tag_value2name.get(tag))
1.90.2.15.2.1  (paf      516:):                      hash.put(StringBody(name), value);
                    517:):              else
                    518:):                      hash.put(StringBody::Format(tag), value);
1.80      paf       519:        }
1.1       paf       520: }
                    521: 
1.90.2.15.2.1  (paf      522:: static void parse_IFD(
                    523:):                    HashStringValue& hash,
                    524:):                    bool is_big, Measure_reader& reader, long tiff_base) {
1.90.2.15.2.5  (paf      525::       const char* buf;
1.90.2.15.2.7  (paf      526::       if(reader.read(buf, sizeof(JPG_Exif_IFD_begin))<sizeof(JPG_Exif_IFD_begin))
1.80      paf       527:                return;
1.90.2.15.2.7  (paf      528::       JPG_Exif_IFD_begin *start=(JPG_Exif_IFD_begin *)buf;
1.80      paf       529: 
                    530:        ushort directory_entry_count=endian_to_ushort(is_big, start->directory_entry_count);
                    531:        for(int i=0; i<directory_entry_count; i++) {
                    532:                if(reader.read(buf, sizeof(JPG_Exif_IFD_entry))<sizeof(JPG_Exif_IFD_entry))
                    533:                        return;
                    534: 
1.90.2.15.2.4  (paf      535::               parse_IFD_entry(hash, is_big, reader, tiff_base, *(JPG_Exif_IFD_entry *)buf);
1.80      paf       536:        }
                    537:        // then goes: LLLLLLLL Offset to next IFD [not going there]
                    538: }
                    539: 
1.90.2.15.2.1  (paf      540:): static Value* parse_exif(Measure_reader& reader, const String& origin_string) {
1.90.2.15.2.5  (paf      541::       const char* buf;
1.90.2.15.2.7  (paf      542::       if(reader.read(buf, sizeof(JPG_Exif_segment_begin))<sizeof(JPG_Exif_segment_begin))
1.80      paf       543:                throw Exception("image.format", 
1.90.2.15.2.1  (paf      544:):                      &origin_string, 
1.80      paf       545:                        "not JPEG file - can not fully read Exif segment start");
                    546: 
1.90.2.15.2.7  (paf      547::       JPG_Exif_segment_begin *start=(JPG_Exif_segment_begin *)buf;
1.80      paf       548:        if(memcmp(start->signature, "Exif\0\0", 4+2)!=0) //signature invalid?
1.90.2.15.2.8  (paf      549::               return 0; // ignore invalid block
1.80      paf       550: 
                    551:        uint tiff_base=reader.tell();
                    552:        if(reader.read(buf, sizeof(JPG_Exif_TIFF_header))<sizeof(JPG_Exif_TIFF_header))
1.90.2.15.2.8  (paf      553::               return 0;
1.80      paf       554: 
                    555:        JPG_Exif_TIFF_header *head=(JPG_Exif_TIFF_header *)buf;
                    556:        bool is_big=head->byte_align_identifier[0]=='M'; // [M]otorola vs [I]ntel
                    557: 
                    558:        uint first_IFD_offset=endian_to_uint(is_big, head->first_IFD_offset);
                    559:        reader.seek(tiff_base+first_IFD_offset, SEEK_SET);
                    560: 
1.90.2.15.2.1  (paf      561:):      VHash* vhash=new VHash;
1.80      paf       562: 
                    563:        // IFD
1.90.2.15.2.1  (paf      564:):      parse_IFD(vhash->hash(), is_big, reader, tiff_base);
1.80      paf       565: 
1.90.2.7  paf       566:        return vhash;
1.80      paf       567: }
                    568: 
1.90.2.15.2.1  (paf      569:): static void measure_jpeg(const String& origin_string, 
1.90.2.15.2.8  (paf      570::                        Measure_reader& reader, ushort& width, ushort& height, Value** exif) {
1.2       paf       571:        // JFIF format markers
1.80      paf       572:        const uchar MARKER=0xFF;
                    573:        const uchar CODE_SIZE_A=0xC0;
                    574:        const uchar CODE_SIZE_B=0xC1;
                    575:        const uchar CODE_SIZE_C=0xC2;
                    576:        const uchar CODE_SIZE_D=0xC3;
                    577:        const uchar CODE_EXIF=0xE1;
1.2       paf       578: 
1.90.2.15.2.5  (paf      579::       const char* buf;
1.18      paf       580:        const size_t prefix_size=2;
1.31      parser    581:        if(reader.read(buf, prefix_size)<prefix_size)
1.68      paf       582:                throw Exception("image.format", 
1.90.2.15.2.1  (paf      583:):                      &origin_string, 
1.34      parser    584:                        "not JPEG file - too small");
1.80      paf       585:        uchar *signature=(uchar *)buf;
1.1       paf       586:        
1.31      parser    587:        if(!(signature[0]==0xFF && signature[1]==0xD8)) 
1.68      paf       588:                throw Exception("image.format", 
1.90.2.15.2.1  (paf      589:):                      &origin_string, 
1.44      parser    590:                        "not JPEG file - wrong signature");
1.31      parser    591: 
                    592:        while(true) {
1.80      paf       593:                uint segment_base=reader.tell()+2/*marker,code*/;
1.31      parser    594:                if(reader.read(buf, sizeof(JPG_Segment_head))<sizeof(JPG_Segment_head))
1.79      paf       595:                        break;
1.31      parser    596:                JPG_Segment_head *head=(JPG_Segment_head *)buf;
                    597: 
                    598:         // Verify that it's a valid segment.
                    599:                if(head->marker!=MARKER)
1.79      paf       600:                        throw Exception("image.format", 
1.90.2.15.2.1  (paf      601:):                              &origin_string, 
1.79      paf       602:                                "not JPEG file - marker not found");
                    603: 
                    604:                switch(head->code) {
                    605:                // http://www.ba.wakwak.com/~tsuruzoh/Computer/Digicams/exif-e.html
                    606:                case CODE_EXIF:
1.80      paf       607:                        if(exif && !*exif) // seen .jpg with some xml under EXIF tag, after real exif block :)
1.90.2.15.2.4  (paf      608::                               *exif=parse_exif(reader, origin_string);
1.1       paf       609:                        break;
1.31      parser    610: 
1.79      paf       611:                case CODE_SIZE_A:
                    612:                case CODE_SIZE_B:
                    613:                case CODE_SIZE_C:
                    614:                case CODE_SIZE_D:
                    615:                        {
                    616:                                // Segments that contain size info
                    617:                                if(reader.read(buf, sizeof(JPG_Size_segment_body))<sizeof(JPG_Size_segment_body))
                    618:                                        throw Exception("image.format", 
1.90.2.15.2.1  (paf      619:):                                              &origin_string, 
1.79      paf       620:                                                "not JPEG file - can not fully read Size segment");
                    621:                                JPG_Size_segment_body *body=(JPG_Size_segment_body *)buf;
                    622:                                
1.80      paf       623:                                width=endian_to_ushort(true, body->width);
                    624:                                height=endian_to_ushort(true, body->height);
1.79      paf       625:                        }                       
1.80      paf       626:                        return;
1.79      paf       627:                };
                    628: 
1.80      paf       629:                reader.seek(segment_base+endian_to_ushort(true, head->length), SEEK_SET);
1.31      parser    630:        }
                    631: 
1.79      paf       632:        throw Exception("image.format", 
1.90.2.15.2.1  (paf      633:):              &origin_string, 
1.79      paf       634:                "broken JPEG file - size frame not found");
1.1       paf       635: }
                    636: 
1.90.2.15.2.1  (paf      637:): static void measure_png(const String& origin_string, 
1.78      paf       638:                         Measure_reader& reader, ushort& width, ushort& height) {
1.72      paf       639: 
1.90.2.15.2.5  (paf      640::       const char* buf;
1.72      paf       641:        const int head_size=sizeof(PNG_Header);
                    642:        if(reader.read(buf, head_size)<head_size)
                    643:                throw Exception("image.format", 
1.90.2.15.2.1  (paf      644:):                      &origin_string, 
1.72      paf       645:                        "not PNG file - too small");
                    646:        PNG_Header *head=(PNG_Header *)buf;
                    647: 
                    648:        if(strncmp(head->signature, "IHDR", 4)!=0)
                    649:                throw Exception("image.format", 
1.90.2.15.2.1  (paf      650:):                      &origin_string, 
1.72      paf       651:                        "not PNG file - wrong signature");      
                    652: 
1.80      paf       653:        width=endian_to_ushort(true, head->width);
                    654:        height=endian_to_ushort(true, head->height);
1.72      paf       655: }
                    656: 
1.1       paf       657: // measure center
                    658: 
1.90.2.15.2.1  (paf      659:): static void measure(const String& file_name, 
1.90.2.15.2.8  (paf      660::                        Measure_reader& reader, ushort& width, ushort& height, Value** exif) {
1.90.2.15.2.1  (paf      661:):      const char* file_name_cstr=file_name.cstr(String::L_FILE_SPEC);
1.90.2.7  paf       662:        if(const char* cext=strrchr(file_name_cstr, '.')) {
1.1       paf       663:                cext++;
                    664:                if(strcasecmp(cext, "GIF")==0)
1.90.2.15.2.4  (paf      665::                       measure_gif(file_name, reader, width, height);
1.1       paf       666:                else if(strcasecmp(cext, "JPG")==0 || strcasecmp(cext, "JPEG")==0) 
1.90.2.15.2.4  (paf      667::                       measure_jpeg(file_name, reader, width, height, exif);
1.72      paf       668:                else if(strcasecmp(cext, "PNG")==0)
1.90.2.15.2.4  (paf      669::                       measure_png(file_name, reader, width, height);
1.1       paf       670:                else
1.68      paf       671:                        throw Exception("image.format", 
1.90.2.15.2.1  (paf      672:):                              &file_name, 
1.1       paf       673:                                "unhandled image file name extension '%s'", cext);
                    674:        } else
1.68      paf       675:                throw Exception("image.format", 
1.90.2.15.2.1  (paf      676:):                      &file_name, 
1.1       paf       677:                        "can not determine image type - no file name extension");
                    678: }
                    679: 
1.77      paf       680: // methods
1.1       paf       681: 
1.40      parser    682: #ifndef DOXYGEN
1.77      paf       683: struct File_measure_action_info {
1.90.2.7  paf       684:        ushort* width;
                    685:        ushort* height;
1.90.2.15.2.8  (paf      686::       Value** exif;
1.90.2.15.2.1  (paf      687:):      const String* file_name;
1.1       paf       688: };
1.40      parser    689: #endif
1.90.2.15.2.1  (paf      690:: static void file_measure_action(
1.80      paf       691:                                                                struct stat& finfo, int f, 
1.90.2.15.2.1  (paf      692::                                                               const String& file_spec, const char* fname, bool as_text,
1.77      paf       693:                                                                void *context) {
                    694:        File_measure_action_info& info=*static_cast<File_measure_action_info *>(context);
1.1       paf       695: 
1.90.2.15.2.1  (paf      696:):      Measure_file_reader reader(f, *info.file_name, fname);
                    697:):      measure(*info.file_name, reader, *info.width, *info.height, info.exif);
1.1       paf       698: }
                    699: 
1.90.2.15.2.1  (paf      700:): static void _measure(Request& r, MethodParams* params) {
                    701:):      Value& data=params->as_no_junction(0, "data must not be code");
1.1       paf       702: 
1.78      paf       703:        ushort width=0;
                    704:        ushort height=0;
1.90.2.15.2.1  (paf      705:):      Value* exif=0;
                    706:):      const String* file_name;
                    707:):      if(file_name=data.get_string()) {
1.90.2.7  paf       708:                File_measure_action_info info;
                    709:                info.width=&width;
                    710:                info.height=&height;
                    711:                info.exif=&exif;
                    712:                info.file_name=file_name;
1.90.2.15.2.1  (paf      713:):              file_read_action_under_lock(r.absolute(*file_name), 
1.77      paf       714:                        "measure", file_measure_action, &info);
1.1       paf       715:        } else {
1.90.2.15.2.1  (paf      716:):              VFile* vfile=data.as_vfile();
                    717:):              file_name=&vfile->fields().get(name_name)->as_string();
1.77      paf       718:                Measure_buf_reader reader(
1.90.2.7  paf       719:                        vfile->value_ptr(),
                    720:                        vfile->value_size(),
1.90.2.15.2.1  (paf      721:):                      *file_name
1.77      paf       722:                );
1.90.2.15.2.1  (paf      723:):              measure(*file_name, reader, width, height, &exif);
1.1       paf       724:        }
                    725: 
1.90.2.15.2.1  (paf      726:):      GET_SELF(r, VImage).set(file_name, width, height, 0, exif);
1.1       paf       727: }
                    728: 
1.40      parser    729: #ifndef DOXYGEN
1.4       paf       730: struct Attrib_info {
1.90.2.7  paf       731:        String* tag; ///< html tag being constructed
                    732:        HashStringValue* skip; ///< tag attributes not to append to tag string [to skip]
1.4       paf       733: };
1.40      parser    734: #endif
1.90.2.7  paf       735: static void append_attrib_pair(
1.90.2.15.2.1  (paf      736:):                             HashStringValue::key_type key, 
                    737:):                             HashStringValue::value_type value, 
                    738:):                             Attrib_info* info) {
1.49      parser    739:        // skip user-specified and internal(starting with "line-") attributes 
1.90.2.15.2.1  (paf      740:):      if(info->skip && info->skip->get(key) || key.pos("line-")==0)
1.4       paf       741:                return;
                    742: 
1.3       paf       743:        // src="a.gif" width=123 ismap[=-1]
1.90.2.7  paf       744:        *info->tag << " " << key;
                    745:        if(value->is_string() || value->as_int()>=0)
1.90.2.15.2.1  (paf      746:):              *info->tag << "=\"" << value->as_string() << "\"";
1.3       paf       747: }
1.90.2.15.2.1  (paf      748:): static void _html(Request& r, MethodParams* params) {
1.3       paf       749: 
1.90.2.7  paf       750:        String tag;
1.3       paf       751:        tag << "<img";
1.4       paf       752: 
1.90.2.7  paf       753:        const HashStringValue& fields=GET_SELF(r, VImage).fields();
                    754:        HashStringValue* attribs=0;
1.4       paf       755: 
1.90.2.10  paf       756:        if(params->count()) {
1.69      paf       757:                // for backward compatibility: someday was ^html{}
1.90.2.15.2.1  (paf      758:):              Value& vattribs=r.process_to_value(*(*params)[0],
1.70      paf       759:                        /*0/*no name* /,*/
1.52      parser    760:                        false/*don't intercept string*/);
1.90.2.15.2.1  (paf      761:):              if(!vattribs.is_string()) // allow empty
                    762:):                      if(attribs=vattribs.get_hash()) {
                    763:):                              Attrib_info info={&tag};
1.90.2.7  paf       764:                                attribs->for_each(append_attrib_pair, &info);
1.39      parser    765:                        } else
1.68      paf       766:                                throw Exception("parser.runtime", 
1.90.2.15.2.1  (paf      767:):                                      0, 
1.39      parser    768:                                        "attributes must be hash");
                    769:        }
1.4       paf       770: 
1.90.2.7  paf       771:        {
1.90.2.15.2.1  (paf      772:):              Attrib_info info={&tag, attribs};
1.90.2.7  paf       773:                fields.for_each(append_attrib_pair, &info);
                    774:        }
1.6       paf       775:        tag << " />";
1.3       paf       776:        r.write_pass_lang(tag);
                    777: }
1.8       paf       778: 
1.68      paf       779: /// @test wrap FILE to auto-object
1.90.2.15.2.1  (paf      780:): static gdImage* load(Request& r, 
                    781::                                        const String& file_name){
                    782:):      const char* file_name_cstr=r.absolute(file_name).cstr(String::L_FILE_SPEC);
1.16      paf       783:        if(FILE *f=fopen(file_name_cstr, "rb")) {
1.90.2.15.2.1  (paf      784:):              gdImage* image=new gdImage;
1.90.2.7  paf       785:                bool ok=image->CreateFromGif(f);
1.16      paf       786:                fclose(f);
1.23      paf       787:                if(!ok)
1.68      paf       788:                        throw Exception("image.format", 
1.90.2.15.2.1  (paf      789:):                              &file_name,
1.23      paf       790:                                "is not in GIF format");
1.90.2.7  paf       791:                return image;
1.16      paf       792:        } else {
1.68      paf       793:                throw Exception("file.missing", 
1.90.2.15.2.1  (paf      794:):                      0, 
                    795:):                      "can not open '%s'", file_name_cstr);
                    796:):              return 0;
1.16      paf       797:        }
                    798: }
                    799: 
                    800: 
1.90.2.15.2.1  (paf      801:): static void _load(Request& r, MethodParams* params) {
                    802::       const String& file_name=params->as_string(0, "file name must not be code");
1.6       paf       803: 
1.90.2.15.2.1  (paf      804:):      gdImage* image=load(r, file_name);
                    805:):      GET_SELF(r, VImage).set(&file_name, image->SX(), image->SY(), image);
1.6       paf       806: }
                    807: 
1.90.2.15.2.1  (paf      808:): static void _create(Request& r, MethodParams* params) {
1.6       paf       809: 
1.90.2.10  paf       810:        int width=params->as_int(0, "width must be int", r);
                    811:        int height=params->as_int(1, "height must be int", r);
1.8       paf       812:        int bgcolor_value=0xffFFff;
1.90.2.10  paf       813:        if(params->count()>2)
                    814:                bgcolor_value=params->as_int(2, "color must be int", r);
1.90.2.15.2.1  (paf      815:):      gdImage* image=new gdImage;
1.90.2.7  paf       816:        image->Create(width, height);
                    817:        image->FilledRectangle(0, 0, width-1, height-1, image->Color(bgcolor_value));
1.90.2.15.2.1  (paf      818::       GET_SELF(r, VImage).set(0, width, height, image);
1.6       paf       819: }
                    820: 
1.90.2.15.2.1  (paf      821:): static void _gif(Request& r, MethodParams* params) {
1.6       paf       822: 
1.90.2.15.2.8  (paf      823::       gdImage* image=GET_SELF(r, VImage).image;
1.8       paf       824:        if(!image)
1.68      paf       825:                throw Exception(0, 
1.90.2.15.2.1  (paf      826:):                      0, 
1.12      paf       827:                        "does not contain an image");
1.6       paf       828: 
                    829:        // could _ but don't thing it's wise to use $image.src for vfile.name
1.10      paf       830: 
1.90.2.15.2.1  (paf      831:):      gdBuf buf=image->Gif();
1.6       paf       832:        
1.90.2.15.2.1  (paf      833:):      VFile& vfile=*new VFile;
                    834:):      Value* content_type=new VString(*new String("image/gif"));
                    835:):      vfile.set(false/*not tainted*/, 
                    836:):              (const char*)buf.ptr, buf.size, 0, content_type);
1.6       paf       837: 
                    838:        r.write_no_lang(vfile);
                    839: }
                    840: 
1.90.2.15.2.1  (paf      841:): static void _line(Request& r, MethodParams* params) {
1.12      paf       842: 
1.90.2.15.2.8  (paf      843::       gdImage* image=GET_SELF(r, VImage).image;
1.12      paf       844:        if(!image)
1.68      paf       845:                throw Exception(0, 
1.90.2.15.2.1  (paf      846:):                      0, 
1.12      paf       847:                        "does not contain an image");
                    848: 
                    849:        image->Line(
1.90.2.10  paf       850:                params->as_int(0, "x0 must be int", r), 
                    851:                params->as_int(1, "y0 must be int", r), 
                    852:                params->as_int(2, "x1 must be int", r), 
                    853:                params->as_int(3, "y1 must be int", r), 
                    854:                image->Color(params->as_int(4, "color must be int", r)));
1.13      paf       855: }
                    856: 
1.90.2.15.2.1  (paf      857:): static void _fill(Request& r, MethodParams* params) {
1.13      paf       858: 
1.90.2.15.2.8  (paf      859::       gdImage* image=GET_SELF(r, VImage).image;
1.13      paf       860:        if(!image)
1.68      paf       861:                throw Exception(0, 
1.90.2.15.2.1  (paf      862:):                      0, 
1.13      paf       863:                        "does not contain an image");
1.12      paf       864: 
1.13      paf       865:        image->Fill(
1.90.2.10  paf       866:                params->as_int(0, "x must be int", r), 
                    867:                params->as_int(1, "y must be int", r), 
                    868:                image->Color(params->as_int(2, "color must be int", r)));
1.13      paf       869: }
                    870: 
1.90.2.15.2.1  (paf      871:): static void _rectangle(Request& r, MethodParams* params) {
1.13      paf       872: 
1.90.2.15.2.8  (paf      873::       gdImage* image=GET_SELF(r, VImage).image;
1.13      paf       874:        if(!image)
1.68      paf       875:                throw Exception(0, 
1.90.2.15.2.1  (paf      876:):                      0, 
1.13      paf       877:                        "does not contain an image");
                    878: 
                    879:        image->Rectangle(
1.90.2.10  paf       880:                params->as_int(0, "x0 must be int", r), 
                    881:                params->as_int(1, "y0 must be int", r), 
                    882:                params->as_int(2, "x1 must be int", r), 
                    883:                params->as_int(3, "y1 must be int", r), 
                    884:                image->Color(params->as_int(4, "color must be int", r)));
1.13      paf       885: }
                    886: 
1.90.2.15.2.1  (paf      887:): static void _bar(Request& r, MethodParams* params) {
1.13      paf       888: 
1.90.2.15.2.8  (paf      889::       gdImage* image=GET_SELF(r, VImage).image;
1.13      paf       890:        if(!image)
1.68      paf       891:                throw Exception(0, 
1.90.2.15.2.1  (paf      892:):                      0, 
1.13      paf       893:                        "does not contain an image");
                    894: 
                    895:        image->FilledRectangle(
1.90.2.10  paf       896:                params->as_int(0, "x0 must be int", r), 
                    897:                params->as_int(1, "y0 must be int", r), 
                    898:                params->as_int(2, "x1 must be int", r), 
                    899:                params->as_int(3, "y1 must be int", r), 
                    900:                image->Color(params->as_int(4, "color must be int", r)));
1.13      paf       901: }
                    902: 
1.44      parser    903: #ifndef DOXYGEN
1.90.2.7  paf       904: static void add_point(Table::element_type row, 
                    905:                                          gdImage::Point **p) {
                    906:        (**p).x=row->get(0)->as_int();
                    907:        (**p).y=row->get(1)->as_int();
1.44      parser    908:        (*p)++;
                    909: }
                    910: #endif
1.90.2.15.2.1  (paf      911:): static void _replace(Request& r, MethodParams* params) {
1.13      paf       912: 
1.90.2.15.2.8  (paf      913::       gdImage* image=GET_SELF(r, VImage).image;
1.13      paf       914:        if(!image)
1.68      paf       915:                throw Exception(0, 
1.90.2.15.2.1  (paf      916:):                      0, 
1.13      paf       917:                        "does not contain an image");
                    918: 
1.90.2.15.2.1  (paf      919:):      Table* table=params->as_no_junction(2, "coordinates must not be code").get_table();
1.44      parser    920:        if(!table) 
1.68      paf       921:                throw Exception(0,
1.90.2.15.2.1  (paf      922:):                      0,
1.44      parser    923:                        "coordinates must be table");
1.13      paf       924: 
1.90.2.15.2.1  (paf      925::       gdImage::Point *all_p=new gdImage::Point[table->count()];
1.44      parser    926:        gdImage::Point *add_p=all_p;    
                    927:        table->for_each(add_point, &add_p);
1.90.2.7  paf       928:        image->FilledPolygonReplaceColor(all_p, table->count(), 
1.90.2.10  paf       929:                image->Color(params->as_int(0, "src color must be int", r)),
                    930:                image->Color(params->as_int(1, "dest color must be int", r)));
1.13      paf       931: }
                    932: 
1.90.2.15.2.1  (paf      933:): static void _polyline(Request& r, MethodParams* params) {
1.13      paf       934: 
1.90.2.15.2.8  (paf      935::       gdImage* image=GET_SELF(r, VImage).image;
1.13      paf       936:        if(!image)
1.68      paf       937:                throw Exception(0, 
1.90.2.15.2.1  (paf      938:):                      0, 
1.13      paf       939:                        "does not contain an image");
                    940: 
1.90.2.15.2.1  (paf      941:):      Table* table=params->as_no_junction(1, "coordinates must not be code").get_table();
1.44      parser    942:        if(!table) 
1.68      paf       943:                throw Exception(0,
1.90.2.15.2.1  (paf      944:):                      0,
1.44      parser    945:                        "coordinates must be table");
                    946: 
1.90.2.15.2.1  (paf      947::       gdImage::Point* all_p=new gdImage::Point[table->count()];
1.44      parser    948:        gdImage::Point *add_p=all_p;    
                    949:        table->for_each(add_point, &add_p);
1.90.2.7  paf       950:        image->Polygon(all_p, table->count(), 
1.90.2.10  paf       951:                image->Color(params->as_int(0, "color must be int", r)),
1.44      parser    952:                false/*not closed*/);
                    953: }
                    954: 
1.90.2.15.2.1  (paf      955:): static void _polygon(Request& r, MethodParams* params) {
1.44      parser    956: 
1.90.2.15.2.8  (paf      957::       gdImage* image=GET_SELF(r, VImage).image;
1.44      parser    958:        if(!image)
1.68      paf       959:                throw Exception(0, 
1.90.2.15.2.1  (paf      960:):                      0, 
1.44      parser    961:                        "does not contain an image");
1.13      paf       962: 
1.90.2.15.2.1  (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.15.2.1  (paf      966:):                      0,
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.13      paf       974: }
                    975: 
1.90.2.15.2.1  (paf      976:): static void _polybar(Request& r, MethodParams* params) {
1.13      paf       977: 
1.90.2.15.2.8  (paf      978::       gdImage* image=GET_SELF(r, VImage).image;
1.13      paf       979:        if(!image)
1.68      paf       980:                throw Exception(0, 
1.90.2.15.2.1  (paf      981:):                      0, 
1.13      paf       982:                        "does not contain an image");
                    983: 
1.90.2.15.2.1  (paf      984:):      Table* table=params->as_no_junction(1, "coordinates must not be code").get_table();
1.44      parser    985:        if(!table) 
1.68      paf       986:                throw Exception("parser.runtime",
1.90.2.15.2.1  (paf      987:):                      0,
1.44      parser    988:                        "coordinates must be table");
1.13      paf       989: 
1.90.2.15.2.1  (paf      990::       gdImage::Point* all_p=new gdImage::Point[table->count()];
1.44      parser    991:        gdImage::Point *add_p=all_p;    
                    992:        table->for_each(add_point, &add_p);
1.90.2.7  paf       993:        image->FilledPolygon(all_p, table->count(), 
1.90.2.10  paf       994:                image->Color(params->as_int(0, "color must be int", r)));
1.12      paf       995: }
                    996: 
1.16      paf       997: // font
                    998: 
1.85      paf       999: #define Y(y)(y+index*height)
1.21      paf      1000: 
1.90.2.15  paf      1001: // Font class
                   1002: 
                   1003: const int Font::letter_spacing=1;
                   1004: 
1.90.2.15.2.1  (paf     1005:: Font::Font(//, 
                   1006::       const String& aalphabet, 
1.90.2.15.2.8  (paf     1007::       gdImage* aifont, int aheight, int amonospace, int aspacebarspace):
1.90.2.15  paf      1008:        alphabet(aalphabet), 
                   1009:        height(aheight), monospace(amonospace),  spacebarspace(aspacebarspace),
                   1010:        ifont(aifont) {
                   1011: }
                   1012: 
                   1013: /* ******************************** char ********************************** */
1.87      paf      1014: 
1.90.2.15.2.1  (paf     1015:): size_t Font::index_of(char ch) {
                   1016:):      if(ch==' ') return STRING_NOT_FOUND;
                   1017:):      return alphabet.pos(ch);
1.90.2.15  paf      1018: }
                   1019: 
1.90.2.15.2.1  (paf     1020:): int Font::index_width(size_t index) {
                   1021:):      if(index==STRING_NOT_FOUND)
1.90.2.15  paf      1022:                return spacebarspace;
                   1023:        int tr=ifont->GetTransparent();
                   1024:        for(int x=ifont->SX()-1; x>=0; x--) {
                   1025:                for(int y=0; y<height; y++)
                   1026:                        if(ifont->GetPixel(x, Y(y))!=tr) 
                   1027:                                return x+1;
1.16      paf      1028:        }
1.90.2.15  paf      1029:        return 0;
                   1030: }
                   1031: 
1.90.2.15.2.1  (paf     1032:): void Font::index_display(gdImage* image, int x, int y, size_t index){
                   1033:):      if(index!=STRING_NOT_FOUND) 
1.90.2.15  paf      1034:                ifont->Copy(*image, x, y, 0, Y(0), index_width(index), height);
                   1035: }
                   1036: 
                   1037: /* ******************************** string ********************************** */
                   1038: 
                   1039: int Font::step_width(int index) {
                   1040:        return letter_spacing + (monospace ? monospace : index_width(index));
                   1041: }
                   1042: 
                   1043: // counts trailing letter_spacing, consider this OK. useful for contiuations
1.90.2.15.2.1  (paf     1044:: int Font::string_width(const String& s){
                   1045:):      const char* cstr=s.cstr();
1.90.2.15  paf      1046:        int result=0;
                   1047:        for(const char* current=cstr; *current; current++)
                   1048:                result+=step_width(index_of(*current));
                   1049:        return result;
                   1050: }
                   1051: 
1.90.2.15.2.8  (paf     1052:: void Font::string_display(gdImage* image, int x, int y, const String& s){
1.90.2.15.2.1  (paf     1053:):      const char* cstr=s.cstr();
1.90.2.15  paf      1054:        for(const char* current=cstr; *current; current++) {
1.90.2.15.2.1  (paf     1055:):              size_t index=index_of(*current);
1.90.2.15  paf      1056:                index_display(image, x, y, index);
                   1057:                x+=step_width(index);
1.16      paf      1058:        }
1.90.2.15  paf      1059: }
                   1060: 
                   1061: //
                   1062: 
1.35      parser   1063: 
1.90.2.15.2.1  (paf     1064:): static void _font(Request& r, MethodParams* params) {
                   1065::       const String& alphabet=params->as_string(0, "alphabet must not be code");
                   1066:):      gdImage* image=load(r, params->as_string(1, "file_name must not be code"));
1.90.2.10  paf      1067:        int spacebar_width=params->as_int(2, "spacebar_width must be int", r);
1.37      parser   1068:        int monospace_width;
1.90.2.10  paf      1069:        if(params->count()>3) {
                   1070:                monospace_width=params->as_int(3, "monospace_width must be int", r);
1.37      parser   1071:                if(!monospace_width)
1.90.2.7  paf      1072:                        monospace_width=image->SX();
1.37      parser   1073:        } else
                   1074:                monospace_width=0;
1.16      paf      1075: 
1.90.2.15.2.1  (paf     1076:):      if(!alphabet.length())
1.68      paf      1077:                throw Exception("parser.runtime",
1.90.2.15.2.1  (paf     1078:):                      0,
1.37      parser   1079:                        "alphabet must not be empty");
1.84      paf      1080: 
1.90.2.15.2.1  (paf     1081:):      if(int remainder=image->SY() % alphabet.length())
1.84      paf      1082:                throw Exception("parser.runtime",
1.90.2.15.2.1  (paf     1083:):                      0,
1.84      paf      1084:                        "font-file height(%d) not divisable by alphabet size(%d), remainder=%d",
1.90.2.15.2.1  (paf     1085:):                              image->SY(), alphabet.length(), remainder);
1.37      parser   1086:        
1.90.2.15.2.1  (paf     1087:):      GET_SELF(r, VImage).font=new Font(
1.37      parser   1088:                alphabet, 
1.36      parser   1089:                image, 
1.90.2.15.2.1  (paf     1090:):              image->SY() / alphabet.length(), monospace_width, spacebar_width);
1.16      paf      1091: }
                   1092: 
1.90.2.15.2.1  (paf     1093:): static void _text(Request& r, MethodParams* params) {
1.90.2.10  paf      1094:        int x=params->as_int(0, "x must be int", r);
                   1095:        int y=params->as_int(1, "y must be int", r);
1.90.2.15.2.1  (paf     1096::       const String& s=params->as_string(2, "text must not be code");
1.16      paf      1097: 
1.90.2.7  paf      1098:        VImage& vimage=GET_SELF(r, VImage);
1.16      paf      1099:        if(vimage.image)
                   1100:                if(vimage.font)
1.90.2.7  paf      1101:                        vimage.font->string_display(vimage.image, x, y, s);
1.16      paf      1102:                else
1.68      paf      1103:                        throw Exception("parser.runtime",
1.90.2.15.2.1  (paf     1104:):                              0,
1.16      paf      1105:                                "set the font first");
                   1106:        else
1.68      paf      1107:                throw Exception(0, 
1.90.2.15.2.1  (paf     1108:):                      0, 
1.16      paf      1109:                        "does not contain an image");
                   1110: }
                   1111: 
1.90.2.15.2.1  (paf     1112:): static void _length(Request& r, MethodParams* params) {
                   1113:):      const String& s=params->as_string(0, "text must not be code");
1.47      parser   1114: 
1.90.2.7  paf      1115:        VImage& vimage=GET_SELF(r, VImage);
1.47      parser   1116:        if(vimage.image)
                   1117:                if(vimage.font) {
1.90.2.15.2.1  (paf     1118:):                      r.write_no_lang(*new VInt(vimage.font->string_width(s)));
1.47      parser   1119:                } else
1.68      paf      1120:                        throw Exception("parser.runtime",
1.90.2.15.2.1  (paf     1121:):                              0,
1.47      parser   1122:                                "set the font first");
                   1123:        else
1.68      paf      1124:                throw Exception(0, 
1.90.2.15.2.1  (paf     1125:):                      0, 
1.47      parser   1126:                        "does not contain an image");
                   1127: }
                   1128: 
1.90.2.15.2.1  (paf     1129:): static void _arc(Request& r, MethodParams* params) {
1.90.2.15.2.8  (paf     1130::       gdImage* image=GET_SELF(r, VImage).image;
1.48      parser   1131:        if(!image)
1.68      paf      1132:                throw Exception(0, 
1.90.2.15.2.1  (paf     1133:):                      0, 
1.48      parser   1134:                        "does not contain an image");
                   1135: 
                   1136:        image->Arc(
1.90.2.10  paf      1137:                params->as_int(0, "center_x must be int", r), 
                   1138:                params->as_int(1, "center_y must be int", r), 
                   1139:                params->as_int(2, "width must be int", r), 
                   1140:                params->as_int(3, "height must be int", r), 
                   1141:                params->as_int(4, "start degrees must be int", r), 
                   1142:                params->as_int(5, "end degrees must be int", r), 
                   1143:                image->Color(params->as_int(6, "cx must be int", r)));
1.48      parser   1144: }
                   1145: 
1.90.2.15.2.1  (paf     1146:): static void _sector(Request& r, MethodParams* params) {
1.90.2.15.2.8  (paf     1147::       gdImage* image=GET_SELF(r, VImage).image;
1.49      parser   1148:        if(!image)
1.68      paf      1149:                throw Exception(0, 
1.90.2.15.2.1  (paf     1150:):                      0, 
1.49      parser   1151:                        "does not contain an image");
                   1152: 
                   1153:        image->Sector(
1.90.2.10  paf      1154:                params->as_int(0, "center_x must be int", r), 
                   1155:                params->as_int(1, "center_y must be int", r), 
                   1156:                params->as_int(2, "width must be int", r), 
                   1157:                params->as_int(3, "height must be int", r), 
                   1158:                params->as_int(4, "start degrees must be int", r), 
                   1159:                params->as_int(5, "end degrees must be int", r), 
                   1160:                image->Color(params->as_int(6, "color must be int", r)));
1.49      parser   1161: }
                   1162: 
1.90.2.15.2.1  (paf     1163:): static void _circle(Request& r, MethodParams* params) {
1.90.2.15.2.8  (paf     1164::       gdImage* image=GET_SELF(r, VImage).image;
1.48      parser   1165:        if(!image)
1.68      paf      1166:                throw Exception(0, 
1.90.2.15.2.1  (paf     1167:):                      0, 
1.48      parser   1168:                        "does not contain an image");
                   1169: 
1.90.2.10  paf      1170:        int size=params->as_int(2, "radius must be int", r)*2;
1.48      parser   1171:        image->Arc(
1.90.2.10  paf      1172:                params->as_int(0, "center_x must be int", r), 
                   1173:                params->as_int(1, "center_y must be int", r), 
1.50      parser   1174:                size, //w
                   1175:                size, //h
1.48      parser   1176:                0, //s
                   1177:                360, //e
1.90.2.10  paf      1178:                image->Color(params->as_int(3, "color must be int", r)));
1.48      parser   1179: }
                   1180: 
1.90.2.15.2.1  (paf     1181:): gdImage* as_image(MethodParams* params, int index, const char* msg) {
                   1182:):      gdImage* src=0;
1.75      paf      1183: 
1.90.2.15.2.1  (paf     1184:):      Value& value=params->as_no_junction(index, msg);
1.53      parser   1185: 
1.90.2.15.2.1  (paf     1186:):      if(Value* vimage=value.as(VIMAGE_TYPE, false)) {
1.75      paf      1187:                src=static_cast<VImage *>(vimage)->image;
                   1188:                if(!src)
                   1189:                        throw Exception("parser.runtime", 
1.90.2.15.2.1  (paf     1190:):                              0, 
1.75      paf      1191:                                msg);
                   1192:        } else
1.68      paf      1193:                throw Exception("parser.runtime", 
1.90.2.15.2.1  (paf     1194:):                      0, 
1.53      parser   1195:                        msg);
                   1196: 
1.90.2.7  paf      1197:        return src;
1.53      parser   1198: }
                   1199: 
1.90.2.15.2.1  (paf     1200:): static void _copy(Request& r, MethodParams* params) {
1.90.2.15.2.8  (paf     1201::       gdImage* dest=GET_SELF(r, VImage).image;
1.53      parser   1202:        if(!dest)
1.68      paf      1203:                throw Exception(0, 
1.90.2.15.2.1  (paf     1204:):                      0, 
1.53      parser   1205:                        "self does not contain an image");
                   1206: 
1.90.2.15.2.1  (paf     1207:):      gdImage* src=as_image(params, 0, "src must be image");
1.53      parser   1208: 
1.90.2.10  paf      1209:        int sx=params->as_int(1, "src_x must be int", r);
                   1210:        int sy=params->as_int(2, "src_y must be int", r);
                   1211:        int sw=params->as_int(3, "src_w must be int", r);
                   1212:        int sh=params->as_int(4, "src_h must be int", r);
                   1213:        int dx=params->as_int(5, "dest_x must be int", r);
                   1214:        int dy=params->as_int(6, "dest_y must be int", r);
                   1215:        if(params->count()>1+2+2+2) {
                   1216:                int dw=params->as_int(1+2+2+2, "dest_w must be int", r);
                   1217:                int dh=(int)(params->count()>1+2+2+2+1?
                   1218:                        params->as_int(1+2+2+2+1, "dest_h must be int", r):sh*(((double)dw)/((double)sw)));
                   1219:                int tolerance=params->count()>1+2+2+2+2?
                   1220:                        params->as_int(1+2+2+2+2, "tolerance must be int", r):150;
1.53      parser   1221: 
1.90.2.7  paf      1222:                src->CopyResampled(*dest, dx, dy, sx, sy, dw, dh, sw, sh, tolerance);
1.53      parser   1223:        } else
1.90.2.7  paf      1224:                src->Copy(*dest, dx, dy, sx, sy, sw, sh);
1.53      parser   1225: }
                   1226: 
                   1227: 
1.22      paf      1228: // constructor
                   1229: 
1.90.2.7  paf      1230: MImage::MImage(): Methoded("image") {
1.1       paf      1231:        // ^image:measure[DATA]
1.22      paf      1232:        add_native_method("measure", Method::CT_DYNAMIC, _measure, 1, 1);
1.3       paf      1233: 
1.25      paf      1234:        // ^image.html[]
                   1235:        // ^image.html[hash]
1.22      paf      1236:        add_native_method("html", Method::CT_DYNAMIC, _html, 0, 1);
1.6       paf      1237: 
1.25      paf      1238:        // ^image.load[background.gif]
1.22      paf      1239:        add_native_method("load", Method::CT_DYNAMIC, _load, 1, 1);
1.6       paf      1240: 
1.25      paf      1241:        // ^image.create[width;height] bgcolor=white
                   1242:        // ^image.create[width;height;bgcolor]
1.22      paf      1243:        add_native_method("create", Method::CT_DYNAMIC, _create, 2, 3);
1.6       paf      1244: 
1.25      paf      1245:        // ^image.gif[]
1.22      paf      1246:        add_native_method("gif", Method::CT_DYNAMIC, _gif, 0, 0);
1.12      paf      1247: 
1.25      paf      1248:        // ^image.line(x0;y0;x1;y1;color)
1.22      paf      1249:        add_native_method("line", Method::CT_DYNAMIC, _line, 5, 5);
1.13      paf      1250: 
1.25      paf      1251:        // ^image.fill(x;y;color)
1.22      paf      1252:        add_native_method("fill", Method::CT_DYNAMIC, _fill, 3, 3);
1.13      paf      1253: 
1.25      paf      1254:        // ^image.rectangle(x0;y0;x1;y1;color)
1.22      paf      1255:        add_native_method("rectangle", Method::CT_DYNAMIC, _rectangle, 5, 5);
1.13      paf      1256: 
1.25      paf      1257:        // ^image.bar(x0;y0;x1;y1;color)
1.22      paf      1258:        add_native_method("bar", Method::CT_DYNAMIC, _bar, 5, 5);
1.13      paf      1259: 
1.44      parser   1260:        // ^image.replace(color-source;color-dest)[table x:y]
                   1261:        add_native_method("replace", Method::CT_DYNAMIC, _replace, 3, 3);
                   1262: 
                   1263:        // ^image.polyline(color)[table x:y]
                   1264:        add_native_method("polyline", Method::CT_DYNAMIC, _polyline, 2, 2);
1.13      paf      1265: 
1.44      parser   1266:        // ^image.polygon(color)[table x:y]
                   1267:        add_native_method("polygon", Method::CT_DYNAMIC, _polygon, 2, 2);
1.13      paf      1268: 
1.44      parser   1269:        // ^image.polybar(color)[table x:y]
                   1270:        add_native_method("polybar", Method::CT_DYNAMIC, _polybar, 2, 2);
1.13      paf      1271: 
1.36      parser   1272:     // ^image.font[alPHAbet;font-file-name.gif](spacebar_width)
                   1273:     // ^image.font[alPHAbet;font-file-name.gif](spacebar_width;width)
                   1274:        add_native_method("font", Method::CT_DYNAMIC, _font, 3, 4);
1.16      paf      1275: 
1.25      paf      1276:     // ^image.text(x;y)[text]
1.22      paf      1277:        add_native_method("text", Method::CT_DYNAMIC, _text, 3, 3);
1.47      parser   1278:        
                   1279:     // ^image.ngth[text]
                   1280:        add_native_method("length", Method::CT_DYNAMIC, _length, 1, 1);
1.16      paf      1281:        
1.48      parser   1282:        // ^image.arc(center x;center y;width;height;start in degrees;end in degrees;color)
                   1283:        add_native_method("arc", Method::CT_DYNAMIC, _arc, 7, 7);
1.49      parser   1284: 
                   1285:        // ^image.sector(center x;center y;width;height;start in degrees;end in degrees;color)
                   1286:        add_native_method("sector", Method::CT_DYNAMIC, _sector, 7, 7);
1.48      parser   1287: 
                   1288:        // ^image.circle(center x;center y;r;color)
                   1289:        add_native_method("circle", Method::CT_DYNAMIC, _circle, 4, 4);
                   1290: 
1.56      parser   1291:        // ^image.copy[source](src x;src y;src w;src h;dst x;dst y[;dest w[;dest h[;tolerance]]])
                   1292:        add_native_method("copy", Method::CT_DYNAMIC, _copy, 1+2+2+2, (1+2+2+2)+2+1);
1.1       paf      1293: }

E-mail: