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