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