Annotation of parser3/src/classes/image.C, revision 1.111
1.1 paf 1: /** @file
2: Parser: @b image parser class.
3:
1.105 paf 4: Copyright(c) 2001-2004 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.111 ! paf 8: static const char * const IDENT_IMAGE_C="$Date: 2004/03/23 15:11:15 $";
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.98 paf 36: Value* create_new_value(Pool&) { 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: const String& file_name; const char* fname;
165: int f;
166:
1.77 paf 167: public:
1.91 paf 168: Measure_file_reader(int af, const String& afile_name, const char* afname):
169: file_name(afile_name), fname(afname), f(af) {
1.1 paf 170: }
171:
1.91 paf 172: override size_t read(const char* &abuf, size_t limit) {
1.77 paf 173: if(limit==0)
1.1 paf 174: return 0;
1.77 paf 175:
1.91 paf 176: char* lbuf=new(PointerFreeGC) char[limit];
1.77 paf 177: size_t read_size=(size_t)::read(f, lbuf, limit); abuf=lbuf;
178: if(ssize_t(read_size)<0 || read_size>limit)
179: throw Exception(0,
180: &file_name,
1.104 paf 181: "measure failed: actually read %u bytes count not in [0..%u] valid range",
1.77 paf 182: read_size, limit);
183:
1.1 paf 184: return read_size;
185: }
186:
1.91 paf 187: override void seek(long value, int whence) {
1.79 paf 188: if(lseek(f, value, whence)<0)
1.88 paf 189: throw Exception("image.format",
1.77 paf 190: &file_name,
1.79 paf 191: "seek(value=%ld, whence=%d) failed: %s (%d), actual filename '%s'",
192: value, whence, strerror(errno), errno, fname);
1.77 paf 193: }
194:
1.91 paf 195: override long tell() { return lseek(f, 0, SEEK_CUR); }
1.79 paf 196:
1.77 paf 197: };
198:
199: class Measure_buf_reader: public Measure_reader {
1.91 paf 200:
201: const char* buf; size_t size;
202: const String& file_name;
203:
204: size_t offset;
205:
1.77 paf 206: public:
1.91 paf 207: Measure_buf_reader(const char* abuf, size_t asize, const String& afile_name):
1.77 paf 208: buf(abuf), size(asize), file_name(afile_name), offset(0) {
209: }
210:
1.91 paf 211: override size_t read(const char* &abuf, size_t limit) {
1.77 paf 212: size_t to_read=min(limit, size-offset);
1.91 paf 213: abuf=buf+offset;
1.77 paf 214: offset+=to_read;
215: return to_read;
216: }
217:
1.91 paf 218: override void seek(long value, int whence) {
1.79 paf 219: size_t new_offset;
220: switch(whence) {
221: case SEEK_CUR: new_offset=offset+value; break;
222: case SEEK_SET: new_offset=(size_t)value; break;
1.91 paf 223: default:
224: throw Exception(0,
225: 0,
226: "whence #%d not supported", 0, whence);
227: break; // never
1.79 paf 228: }
229:
1.77 paf 230: if((ssize_t)new_offset<0 || new_offset>size)
1.88 paf 231: throw Exception("image.format",
1.77 paf 232: &file_name,
1.79 paf 233: "seek(value=%l, whence=%d) failed: out of buffer, new_offset>size (%l>%l) or new_offset<0",
234: value, whence, new_offset, size);
1.77 paf 235: offset=new_offset;
236: }
237:
1.91 paf 238: override long tell() { return offset; }
1.79 paf 239:
1.1 paf 240: };
1.77 paf 241:
1.43 parser 242: #endif
1.1 paf 243:
1.72 paf 244: /// PNG file header
245: struct PNG_Header {
246: char dummy[12];
247: char signature[4]; //< must be "IHDR"
1.80 paf 248: uchar high_width[2]; //< image width high bytes [we ignore for now]
249: uchar width[2]; //< image width low bytes
250: uchar high_height[2]; //< image height high bytes [we ignore for now]
251: uchar height[4]; //< image height
1.72 paf 252: };
253:
1.21 paf 254: /// GIF file header
1.1 paf 255: struct GIF_Header {
1.72 paf 256: char signature[3]; // 'GIF'
1.1 paf 257: char version[3];
1.80 paf 258: uchar width[2];
259: uchar height[2];
1.1 paf 260: char dif;
261: char fonColor;
262: char nulls;
263: };
264:
1.31 parser 265: /// JPEG record head
266: struct JPG_Segment_head {
1.80 paf 267: uchar marker;
268: uchar code;
269: uchar length[2];
1.1 paf 270: };
1.21 paf 271: /// JPEG frame header
1.31 parser 272: struct JPG_Size_segment_body {
1.27 parser 273: char data; //< data precision of bits/sample
1.80 paf 274: uchar height[2]; //< image height
275: uchar width[2]; //< image width
1.27 parser 276: char numComponents; //< number of color components
1.1 paf 277: };
278:
1.79 paf 279: /// JPEG frame header
1.91 paf 280: struct JPG_Exif_segment_begin {
1.79 paf 281: char signature[6]; // Exif\0\0
282: };
283:
1.80 paf 284: /// JPEG Exif TIFF Header
285: struct JPG_Exif_TIFF_header {
286: uchar byte_align_identifier[2];
287: char dummy[2]; // always 000A [or 0A00]
288: uchar first_IFD_offset[4]; // Usually the first IFD starts immediately next to TIFF header, so this offset has value '0x00000008'.
289: };
290:
291: // JPEG Exif IFD start
1.91 paf 292: struct JPG_Exif_IFD_begin {
1.80 paf 293: uchar directory_entry_count[2]; // the number of directory entry contains in this IFD
294: };
295:
296: // TTTT ffff NNNNNNNN DDDDDDDD
297: struct JPG_Exif_IFD_entry {
298: uchar tag[2]; // Tag number, this shows a kind of data
299: uchar format[2]; // data format
300: uchar components_count[4]; // number of components
301: uchar value_or_offset_to_it[4]; // data value or offset to data value
302: };
303:
304: #define JPG_IFD_TAG_EXIF_OFFSET 0x8769
305:
1.89 paf 306: #define JPEG_EXIF_DATE_CHARS 20
307:
1.1 paf 308: //
309:
1.80 paf 310: inline ushort x_endian_to_ushort(uchar b0, uchar b1) {
311: return (ushort)((b1<<8) + b0);
1.33 parser 312: }
313:
1.80 paf 314: inline uint x_endian_to_uint(uchar b0, uchar b1, uchar b2, uchar b3) {
315: return (uint)(((((b3<<8) + b2)<<8)+b1)<<8)+b0;
1.33 parser 316: }
317:
1.80 paf 318: inline ushort endian_to_ushort(bool is_big, const uchar *b/* [2] */) {
319: return is_big?x_endian_to_ushort(b[1], b[0]):
320: x_endian_to_ushort(b[0], b[1]);
1.1 paf 321: }
322:
1.80 paf 323: inline uint endian_to_uint(bool is_big, const uchar *b /* [4] */) {
324: return is_big?x_endian_to_uint(b[3], b[2], b[1], b[0]):
325: x_endian_to_uint(b[0], b[1], b[2], b[3]);
326: }
327:
1.91 paf 328: static void measure_gif(const String& origin_string,
1.78 paf 329: Measure_reader& reader, ushort& width, ushort& height) {
1.1 paf 330:
1.91 paf 331: const char* buf;
1.102 paf 332: const size_t head_size=sizeof(GIF_Header);
333: if(reader.read(buf, head_size)<head_size)
1.68 paf 334: throw Exception("image.format",
1.91 paf 335: &origin_string,
1.34 parser 336: "not GIF file - too small");
1.31 parser 337: GIF_Header *head=(GIF_Header *)buf;
1.1 paf 338:
1.72 paf 339: if(strncmp(head->signature, "GIF", 3)!=0)
1.68 paf 340: throw Exception("image.format",
1.91 paf 341: &origin_string,
1.44 parser 342: "not GIF file - wrong signature");
1.1 paf 343:
1.80 paf 344: width=endian_to_ushort(false, head->width);
345: height=endian_to_ushort(false, head->height);
346: }
347:
1.91 paf 348: static Value* parse_IFD_entry_formatted_one_value(
1.80 paf 349: bool is_big,
350: ushort format,
351: size_t component_size,
352: const uchar *value) {
353: switch(format) {
354: case 1: // unsigned byte
1.91 paf 355: return new VInt((uchar)value[0]);
1.80 paf 356: case 3: // unsigned short
1.91 paf 357: return new VInt(endian_to_ushort(is_big, value));
1.80 paf 358: case 4: // unsigned long
359: // 'double' because parser's Int is signed
1.91 paf 360: return new VDouble(endian_to_uint(is_big, value));
1.80 paf 361: case 5: // unsigned rational
362: {
363: uint numerator=endian_to_uint(is_big, value); value+=component_size/2;
364: uint denominator=endian_to_uint(is_big, value);
365: if(!denominator)
366: return 0;
1.91 paf 367: return new VDouble(((double)numerator)/denominator);
1.80 paf 368: }
369: case 6: // signed byte
1.91 paf 370: return new VInt((signed char)value[0]);
1.80 paf 371: case 8: // signed short
1.91 paf 372: return new VInt((signed short)endian_to_ushort(is_big, value));
1.80 paf 373: case 9: // signed long
1.91 paf 374: return new VInt((signed int)endian_to_uint(is_big, value));
1.80 paf 375: case 10: // signed rational
376: {
377: signed int numerator=(signed int)endian_to_uint(is_big, value); value+=component_size/2;
378: uint denominator=endian_to_uint(is_big, value);
379: if(!denominator)
380: return 0;
1.91 paf 381: return new VDouble(numerator/denominator);
1.80 paf 382: }
383: /*
384: case 11: // single float
385: todo
386: case 12: // double float
387: todo
388: */
389: };
390:
391: return 0;
392: }
393:
1.89 paf 394: // date.C
1.107 paf 395: tm cstr_to_time_t(char *cstr);
1.89 paf 396:
1.91 paf 397: static Value* parse_IFD_entry_formatted_value(bool is_big, ushort format,
398: size_t component_size, uint components_count,
399: const uchar *value) {
1.80 paf 400: if(format==2) { // ascii string, exception: the only type with varying size
1.91 paf 401: const char* cstr=(const char* )value;
402: size_t length=components_count;
1.89 paf 403: // Data format is "YYYY:MM:DD HH:MM:SS"+0x00, total 20bytes
1.91 paf 404: if(length==JPEG_EXIF_DATE_CHARS
1.111 ! paf 405: && isdigit((unsigned char)cstr[0])
1.91 paf 406: && cstr[length-1]==0) {
1.89 paf 407: char cstr_writable[JPEG_EXIF_DATE_CHARS];
408: strcpy(cstr_writable, cstr);
409:
1.106 paf 410: try {
411: return new VDate(cstr_to_time_t(cstr_writable));
412: }
413: catch(...) { /*ignore bad date times*/ }
1.89 paf 414: }
415:
1.91 paf 416: if(const char* premature_zero_pos=(const char* )memchr(cstr, 0, length))
417: length=premature_zero_pos-cstr;
418: return new VString(*new String(cstr, length, true/*tainted*/));
1.80 paf 419: }
420:
421: if(components_count==1)
1.91 paf 422: return parse_IFD_entry_formatted_one_value(is_big, format, component_size, value);
1.80 paf 423:
1.91 paf 424: VHash* result=new VHash;
425: HashStringValue& hash=result->hash();
1.80 paf 426: for(uint i=0; i<components_count; i++, value+=component_size) {
1.91 paf 427: hash.put(
1.93 paf 428: String::Body::Format(i),
1.91 paf 429: parse_IFD_entry_formatted_one_value(is_big, format, component_size, value));
1.80 paf 430: }
431:
1.91 paf 432: return result;
1.80 paf 433: }
434:
1.91 paf 435: static Value* parse_IFD_entry_value(
1.80 paf 436: bool is_big, Measure_reader& reader, long tiff_base,
437: JPG_Exif_IFD_entry& entry) {
438: size_t format2component_size[]={
439: 0, // undefined
440: 1, // unsigned byte
441: 1, // ascii string
442: 2, // unsigned short
443: 4, // unsigned long
444: 8, // unsigned rational
445: 1, // signed byte
446: 0, // undefined
447: 2, // signed short
448: 4, // signed long
449: 8, // signed rational
450: /*
451: 4, // single float
452: 8, // double float
453: */
454: };
455:
456: ushort format=endian_to_ushort(is_big, entry.format);
457: if(format>=sizeof(format2component_size)/sizeof(format2component_size[0]))
458: return 0; // format out of range, ignoring
459:
460: size_t component_size=format2component_size[format];
461: if(component_size==0)
462: return 0; // undefined format
463:
464: // You can get the total data byte length by multiplies
465: // a 'bytes/components' value (see above chart) by number of components stored 'NNNNNNNN' area
466: uint components_count=endian_to_uint(is_big, entry.components_count);
467: size_t value_size=component_size*components_count;
468: // If its size is over 4bytes, 'DDDDDDDD' contains the offset to data stored address
1.91 paf 469: Value* result;
1.80 paf 470:
471: if(value_size<=4)
1.91 paf 472: result=parse_IFD_entry_formatted_value(
1.80 paf 473: is_big, format,
474: component_size, components_count,
475: entry.value_or_offset_to_it);
476: else {
477: long remembered=reader.tell();
478: {
479: reader.seek(tiff_base+endian_to_uint(is_big, entry.value_or_offset_to_it), SEEK_SET);
1.91 paf 480: const char* value;
1.80 paf 481: if(reader.read(value, value_size)<sizeof(value_size))
482: return 0;
1.91 paf 483: result=parse_IFD_entry_formatted_value(
1.80 paf 484: is_big, format,
485: component_size, components_count,
486: (const uchar*)value);
487: }
488: reader.seek(remembered, SEEK_SET);
489: }
490:
491: return result;
492: }
493:
1.91 paf 494: static void parse_IFD(HashStringValue& hash,
495: bool is_big, Measure_reader& reader, long tiff_base);
496:
497: static void parse_IFD_entry(HashStringValue& hash,
498: bool is_big, Measure_reader& reader, long tiff_base,
499: JPG_Exif_IFD_entry& entry) {
1.80 paf 500: ushort tag=endian_to_ushort(is_big, entry.tag);
501: if(tag==JPG_IFD_TAG_EXIF_OFFSET) {
502: long remembered=reader.tell();
503: {
504: reader.seek(tiff_base+endian_to_uint(is_big, entry.value_or_offset_to_it), SEEK_SET);
1.91 paf 505: parse_IFD(hash, is_big, reader, tiff_base);
1.80 paf 506: }
507: reader.seek(remembered, SEEK_SET);
508: return;
509: }
510:
1.91 paf 511: if(Value* value=parse_IFD_entry_value(is_big, reader, tiff_base, entry)) {
512: if(const char* name=exif_tag_value2name.get(tag))
1.93 paf 513: hash.put(String::Body(name), value);
1.80 paf 514: else
1.93 paf 515: hash.put(String::Body::Format(tag), value);
1.80 paf 516: }
1.1 paf 517: }
518:
1.91 paf 519: static void parse_IFD(
520: HashStringValue& hash,
521: bool is_big, Measure_reader& reader, long tiff_base) {
522: const char* buf;
523: if(reader.read(buf, sizeof(JPG_Exif_IFD_begin))<sizeof(JPG_Exif_IFD_begin))
1.80 paf 524: return;
1.91 paf 525: JPG_Exif_IFD_begin *start=(JPG_Exif_IFD_begin *)buf;
1.80 paf 526:
527: ushort directory_entry_count=endian_to_ushort(is_big, start->directory_entry_count);
528: for(int i=0; i<directory_entry_count; i++) {
529: if(reader.read(buf, sizeof(JPG_Exif_IFD_entry))<sizeof(JPG_Exif_IFD_entry))
530: return;
531:
1.91 paf 532: parse_IFD_entry(hash, is_big, reader, tiff_base, *(JPG_Exif_IFD_entry *)buf);
1.80 paf 533: }
534: // then goes: LLLLLLLL Offset to next IFD [not going there]
535: }
536:
1.91 paf 537: static Value* parse_exif(Measure_reader& reader, const String& origin_string) {
538: const char* buf;
539: if(reader.read(buf, sizeof(JPG_Exif_segment_begin))<sizeof(JPG_Exif_segment_begin))
1.80 paf 540: throw Exception("image.format",
1.91 paf 541: &origin_string,
1.80 paf 542: "not JPEG file - can not fully read Exif segment start");
543:
1.91 paf 544: JPG_Exif_segment_begin *start=(JPG_Exif_segment_begin *)buf;
1.80 paf 545: if(memcmp(start->signature, "Exif\0\0", 4+2)!=0) //signature invalid?
546: return 0; // ignore invalid block
547:
548: uint tiff_base=reader.tell();
549: if(reader.read(buf, sizeof(JPG_Exif_TIFF_header))<sizeof(JPG_Exif_TIFF_header))
550: return 0;
551:
552: JPG_Exif_TIFF_header *head=(JPG_Exif_TIFF_header *)buf;
553: bool is_big=head->byte_align_identifier[0]=='M'; // [M]otorola vs [I]ntel
554:
555: uint first_IFD_offset=endian_to_uint(is_big, head->first_IFD_offset);
556: reader.seek(tiff_base+first_IFD_offset, SEEK_SET);
557:
1.91 paf 558: VHash* vhash=new VHash;
1.80 paf 559:
560: // IFD
1.91 paf 561: parse_IFD(vhash->hash(), is_big, reader, tiff_base);
1.80 paf 562:
1.91 paf 563: return vhash;
1.80 paf 564: }
565:
1.91 paf 566: static void measure_jpeg(const String& origin_string,
567: Measure_reader& reader, ushort& width, ushort& height, Value** exif) {
1.2 paf 568: // JFIF format markers
1.80 paf 569: const uchar MARKER=0xFF;
570: const uchar CODE_SIZE_A=0xC0;
571: const uchar CODE_SIZE_B=0xC1;
572: const uchar CODE_SIZE_C=0xC2;
573: const uchar CODE_SIZE_D=0xC3;
574: const uchar CODE_EXIF=0xE1;
1.2 paf 575:
1.91 paf 576: const char* buf;
1.18 paf 577: const size_t prefix_size=2;
1.31 parser 578: if(reader.read(buf, prefix_size)<prefix_size)
1.68 paf 579: throw Exception("image.format",
1.91 paf 580: &origin_string,
1.34 parser 581: "not JPEG file - too small");
1.80 paf 582: uchar *signature=(uchar *)buf;
1.1 paf 583:
1.31 parser 584: if(!(signature[0]==0xFF && signature[1]==0xD8))
1.68 paf 585: throw Exception("image.format",
1.91 paf 586: &origin_string,
1.44 parser 587: "not JPEG file - wrong signature");
1.31 parser 588:
589: while(true) {
1.80 paf 590: uint segment_base=reader.tell()+2/*marker,code*/;
1.31 parser 591: if(reader.read(buf, sizeof(JPG_Segment_head))<sizeof(JPG_Segment_head))
1.79 paf 592: break;
1.31 parser 593: JPG_Segment_head *head=(JPG_Segment_head *)buf;
594:
595: // Verify that it's a valid segment.
596: if(head->marker!=MARKER)
1.79 paf 597: throw Exception("image.format",
1.91 paf 598: &origin_string,
1.79 paf 599: "not JPEG file - marker not found");
600:
601: switch(head->code) {
602: // http://www.ba.wakwak.com/~tsuruzoh/Computer/Digicams/exif-e.html
603: case CODE_EXIF:
1.80 paf 604: if(exif && !*exif) // seen .jpg with some xml under EXIF tag, after real exif block :)
1.91 paf 605: *exif=parse_exif(reader, origin_string);
1.1 paf 606: break;
1.31 parser 607:
1.79 paf 608: case CODE_SIZE_A:
609: case CODE_SIZE_B:
610: case CODE_SIZE_C:
611: case CODE_SIZE_D:
612: {
613: // Segments that contain size info
614: if(reader.read(buf, sizeof(JPG_Size_segment_body))<sizeof(JPG_Size_segment_body))
615: throw Exception("image.format",
1.91 paf 616: &origin_string,
1.79 paf 617: "not JPEG file - can not fully read Size segment");
618: JPG_Size_segment_body *body=(JPG_Size_segment_body *)buf;
619:
1.80 paf 620: width=endian_to_ushort(true, body->width);
621: height=endian_to_ushort(true, body->height);
1.79 paf 622: }
1.80 paf 623: return;
1.79 paf 624: };
625:
1.80 paf 626: reader.seek(segment_base+endian_to_ushort(true, head->length), SEEK_SET);
1.31 parser 627: }
628:
1.79 paf 629: throw Exception("image.format",
1.91 paf 630: &origin_string,
1.79 paf 631: "broken JPEG file - size frame not found");
1.1 paf 632: }
633:
1.91 paf 634: static void measure_png(const String& origin_string,
1.78 paf 635: Measure_reader& reader, ushort& width, ushort& height) {
1.72 paf 636:
1.91 paf 637: const char* buf;
1.102 paf 638: const size_t head_size=sizeof(PNG_Header);
639: if(reader.read(buf, head_size)<head_size)
1.72 paf 640: throw Exception("image.format",
1.91 paf 641: &origin_string,
1.72 paf 642: "not PNG file - too small");
643: PNG_Header *head=(PNG_Header *)buf;
644:
645: if(strncmp(head->signature, "IHDR", 4)!=0)
646: throw Exception("image.format",
1.91 paf 647: &origin_string,
1.72 paf 648: "not PNG file - wrong signature");
649:
1.80 paf 650: width=endian_to_ushort(true, head->width);
651: height=endian_to_ushort(true, head->height);
1.72 paf 652: }
653:
1.1 paf 654: // measure center
655:
1.91 paf 656: static void measure(const String& file_name,
657: Measure_reader& reader, ushort& width, ushort& height, Value** exif) {
658: const char* file_name_cstr=file_name.cstr(String::L_FILE_SPEC);
659: if(const char* cext=strrchr(file_name_cstr, '.')) {
1.1 paf 660: cext++;
661: if(strcasecmp(cext, "GIF")==0)
1.91 paf 662: measure_gif(file_name, reader, width, height);
1.1 paf 663: else if(strcasecmp(cext, "JPG")==0 || strcasecmp(cext, "JPEG")==0)
1.91 paf 664: measure_jpeg(file_name, reader, width, height, exif);
1.72 paf 665: else if(strcasecmp(cext, "PNG")==0)
1.91 paf 666: measure_png(file_name, reader, width, height);
1.1 paf 667: else
1.68 paf 668: throw Exception("image.format",
1.1 paf 669: &file_name,
670: "unhandled image file name extension '%s'", cext);
671: } else
1.68 paf 672: throw Exception("image.format",
1.1 paf 673: &file_name,
674: "can not determine image type - no file name extension");
675: }
676:
1.77 paf 677: // methods
1.1 paf 678:
1.40 parser 679: #ifndef DOXYGEN
1.77 paf 680: struct File_measure_action_info {
1.91 paf 681: ushort* width;
682: ushort* height;
683: Value** exif;
684: const String* file_name;
1.1 paf 685: };
1.40 parser 686: #endif
1.91 paf 687: static void file_measure_action(
1.98 paf 688: struct stat& /*finfo*/, int f,
689: const String& /*file_spec*/, const char* fname, bool /*as_text*/,
1.77 paf 690: void *context) {
691: File_measure_action_info& info=*static_cast<File_measure_action_info *>(context);
1.1 paf 692:
1.91 paf 693: Measure_file_reader reader(f, *info.file_name, fname);
694: measure(*info.file_name, reader, *info.width, *info.height, info.exif);
1.1 paf 695: }
696:
1.91 paf 697: static void _measure(Request& r, MethodParams& params) {
698: Value& data=params.as_no_junction(0, "data must not be code");
1.1 paf 699:
1.78 paf 700: ushort width=0;
701: ushort height=0;
1.91 paf 702: Value* exif=0;
703: const String* file_name;
1.100 paf 704: if((file_name=data.get_string())) {
705: File_measure_action_info info={
706: &width, &height,
707: &exif,
708: file_name
709: };
1.91 paf 710: file_read_action_under_lock(r.absolute(*file_name),
1.77 paf 711: "measure", file_measure_action, &info);
1.1 paf 712: } else {
1.91 paf 713: VFile* vfile=data.as_vfile();
714: file_name=&vfile->fields().get(name_name)->as_string();
1.77 paf 715: Measure_buf_reader reader(
1.91 paf 716: vfile->value_ptr(),
717: vfile->value_size(),
1.77 paf 718: *file_name
719: );
1.91 paf 720: measure(*file_name, reader, width, height, &exif);
1.1 paf 721: }
722:
1.91 paf 723: GET_SELF(r, VImage).set(file_name, width, height, 0, exif);
1.1 paf 724: }
725:
1.40 parser 726: #ifndef DOXYGEN
1.4 paf 727: struct Attrib_info {
1.91 paf 728: String* tag; ///< html tag being constructed
729: HashStringValue* skip; ///< tag attributes not to append to tag string [to skip]
1.4 paf 730: };
1.40 parser 731: #endif
1.91 paf 732: static void append_attrib_pair(
733: HashStringValue::key_type key,
734: HashStringValue::value_type value,
735: Attrib_info* info) {
1.49 parser 736: // skip user-specified and internal(starting with "line-") attributes
1.91 paf 737: if(info->skip && info->skip->get(key) || key.pos("line-")==0)
1.4 paf 738: return;
739:
1.3 paf 740: // src="a.gif" width=123 ismap[=-1]
1.91 paf 741: *info->tag << " " << key;
742: if(value->is_string() || value->as_int()>=0)
743: *info->tag << "=\"" << value->as_string() << "\"";
1.3 paf 744: }
1.91 paf 745: static void _html(Request& r, MethodParams& params) {
1.3 paf 746:
1.91 paf 747: String tag;
1.3 paf 748: tag << "<img";
1.4 paf 749:
1.91 paf 750: const HashStringValue& fields=GET_SELF(r, VImage).fields();
751: HashStringValue* attribs=0;
1.4 paf 752:
1.91 paf 753: if(params.count()) {
1.69 paf 754: // for backward compatibility: someday was ^html{}
1.91 paf 755: Value& vattribs=r.process_to_value(params[0],
1.52 parser 756: false/*don't intercept string*/);
1.82 paf 757: if(!vattribs.is_string()) // allow empty
1.100 paf 758: if((attribs=vattribs.get_hash())) {
759: Attrib_info info={&tag, 0};
1.91 paf 760: attribs->for_each(append_attrib_pair, &info);
1.39 parser 761: } else
1.68 paf 762: throw Exception("parser.runtime",
1.91 paf 763: 0,
1.39 parser 764: "attributes must be hash");
765: }
1.4 paf 766:
1.91 paf 767: {
768: Attrib_info info={&tag, attribs};
769: fields.for_each(append_attrib_pair, &info);
770: }
1.6 paf 771: tag << " />";
1.3 paf 772: r.write_pass_lang(tag);
773: }
1.8 paf 774:
1.68 paf 775: /// @test wrap FILE to auto-object
1.91 paf 776: static gdImage* load(Request& r,
1.16 paf 777: const String& file_name){
1.91 paf 778: const char* file_name_cstr=r.absolute(file_name).cstr(String::L_FILE_SPEC);
1.16 paf 779: if(FILE *f=fopen(file_name_cstr, "rb")) {
1.91 paf 780: gdImage* image=new gdImage;
781: bool ok=image->CreateFromGif(f);
1.16 paf 782: fclose(f);
1.23 paf 783: if(!ok)
1.68 paf 784: throw Exception("image.format",
1.23 paf 785: &file_name,
786: "is not in GIF format");
1.91 paf 787: return image;
1.16 paf 788: } else {
1.68 paf 789: throw Exception("file.missing",
1.91 paf 790: 0,
1.16 paf 791: "can not open '%s'", file_name_cstr);
792: }
793: }
794:
795:
1.91 paf 796: static void _load(Request& r, MethodParams& params) {
797: const String& file_name=params.as_string(0, "file name must not be code");
1.6 paf 798:
1.91 paf 799: gdImage* image=load(r, file_name);
800: GET_SELF(r, VImage).set(&file_name, image->SX(), image->SY(), image);
1.6 paf 801: }
802:
1.91 paf 803: static void _create(Request& r, MethodParams& params) {
804: int width=params.as_int(0, "width must be int", r);
805: int height=params.as_int(1, "height must be int", r);
1.8 paf 806: int bgcolor_value=0xffFFff;
1.91 paf 807: if(params.count()>2)
808: bgcolor_value=params.as_int(2, "color must be int", r);
809: gdImage* image=new gdImage;
810: image->Create(width, height);
811: image->FilledRectangle(0, 0, width-1, height-1, image->Color(bgcolor_value));
812: GET_SELF(r, VImage).set(0, width, height, image);
1.6 paf 813: }
814:
1.91 paf 815: static void _gif(Request& r, MethodParams& params) {
1.108 paf 816: gdImage& image=GET_SELF(r, VImage).image();
1.6 paf 817:
1.95 paf 818: const String *file_name=0;
819: if(params.count()>0)
820: file_name=¶ms.as_string(0, "file name must be string");
1.10 paf 821:
1.108 paf 822: gdBuf buf=image.Gif();
1.6 paf 823:
1.91 paf 824: VFile& vfile=*new VFile;
825: Value* content_type=new VString(*new String("image/gif"));
1.20 paf 826: vfile.set(false/*not tainted*/,
1.95 paf 827: (const char*)buf.ptr, buf.size,
828: file_name? file_name->cstr(String::L_FILE_SPEC): 0,
829: content_type);
1.6 paf 830:
831: r.write_no_lang(vfile);
832: }
833:
1.91 paf 834: static void _line(Request& r, MethodParams& params) {
1.108 paf 835: gdImage& image=GET_SELF(r, VImage).image();
1.12 paf 836:
1.108 paf 837: image.Line(
1.91 paf 838: params.as_int(0, "x0 must be int", r),
839: params.as_int(1, "y0 must be int", r),
840: params.as_int(2, "x1 must be int", r),
841: params.as_int(3, "y1 must be int", r),
1.108 paf 842: image.Color(params.as_int(4, "color must be int", r)));
1.13 paf 843: }
844:
1.91 paf 845: static void _fill(Request& r, MethodParams& params) {
1.108 paf 846: gdImage& image=GET_SELF(r, VImage).image();
1.12 paf 847:
1.108 paf 848: image.Fill(
1.91 paf 849: params.as_int(0, "x must be int", r),
850: params.as_int(1, "y must be int", r),
1.108 paf 851: image.Color(params.as_int(2, "color must be int", r)));
1.13 paf 852: }
853:
1.91 paf 854: static void _rectangle(Request& r, MethodParams& params) {
1.108 paf 855: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 856:
1.108 paf 857: image.Rectangle(
1.91 paf 858: params.as_int(0, "x0 must be int", r),
859: params.as_int(1, "y0 must be int", r),
860: params.as_int(2, "x1 must be int", r),
861: params.as_int(3, "y1 must be int", r),
1.108 paf 862: image.Color(params.as_int(4, "color must be int", r)));
1.13 paf 863: }
864:
1.91 paf 865: static void _bar(Request& r, MethodParams& params) {
1.108 paf 866: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 867:
1.108 paf 868: image.FilledRectangle(
1.91 paf 869: params.as_int(0, "x0 must be int", r),
870: params.as_int(1, "y0 must be int", r),
871: params.as_int(2, "x1 must be int", r),
872: params.as_int(3, "y1 must be int", r),
1.108 paf 873: image.Color(params.as_int(4, "color must be int", r)));
1.13 paf 874: }
875:
1.44 parser 876: #ifndef DOXYGEN
1.91 paf 877: static void add_point(Table::element_type row,
878: gdImage::Point **p) {
1.110 paf 879: if(row->count()!=2)
880: throw Exception(0,
881: 0,
882: "coordinates table must contain two columns: x and y values");
1.91 paf 883: (**p).x=row->get(0)->as_int();
884: (**p).y=row->get(1)->as_int();
1.44 parser 885: (*p)++;
886: }
887: #endif
1.91 paf 888: static void _replace(Request& r, MethodParams& params) {
1.108 paf 889: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 890:
1.91 paf 891: Table* table=params.as_no_junction(2, "coordinates must not be code").get_table();
1.44 parser 892: if(!table)
1.68 paf 893: throw Exception(0,
1.91 paf 894: 0,
1.44 parser 895: "coordinates must be table");
1.13 paf 896:
1.92 paf 897: gdImage::Point *all_p=new(PointerFreeGC) gdImage::Point[table->count()];
1.44 parser 898: gdImage::Point *add_p=all_p;
899: table->for_each(add_point, &add_p);
1.108 paf 900: image.FilledPolygonReplaceColor(all_p, table->count(),
901: image.Color(params.as_int(0, "src color must be int", r)),
902: image.Color(params.as_int(1, "dest color must be int", r)));
1.13 paf 903: }
904:
1.91 paf 905: static void _polyline(Request& r, MethodParams& params) {
1.108 paf 906: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 907:
1.91 paf 908: Table* table=params.as_no_junction(1, "coordinates must not be code").get_table();
1.44 parser 909: if(!table)
1.68 paf 910: throw Exception(0,
1.91 paf 911: 0,
1.44 parser 912: "coordinates must be table");
913:
1.92 paf 914: gdImage::Point* all_p=new(PointerFreeGC) gdImage::Point[table->count()];
1.44 parser 915: gdImage::Point *add_p=all_p;
916: table->for_each(add_point, &add_p);
1.108 paf 917: image.Polygon(all_p, table->count(),
918: image.Color(params.as_int(0, "color must be int", r)),
1.44 parser 919: false/*not closed*/);
920: }
921:
1.91 paf 922: static void _polygon(Request& r, MethodParams& params) {
1.108 paf 923: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 924:
1.91 paf 925: Table* table=params.as_no_junction(1, "coordinates must not be code").get_table();
1.44 parser 926: if(!table)
1.68 paf 927: throw Exception(0,
1.91 paf 928: 0,
1.44 parser 929: "coordinates must be table");
930:
1.92 paf 931: gdImage::Point* all_p=new(PointerFreeGC) gdImage::Point[table->count()];
1.44 parser 932: gdImage::Point *add_p=all_p;
933: table->for_each(add_point, &add_p);
1.108 paf 934: image.Polygon(all_p, table->count(),
935: image.Color(params.as_int(0, "color must be int", r)));
1.13 paf 936: }
937:
1.91 paf 938: static void _polybar(Request& r, MethodParams& params) {
1.108 paf 939: gdImage& image=GET_SELF(r, VImage).image();
1.13 paf 940:
1.91 paf 941: Table* table=params.as_no_junction(1, "coordinates must not be code").get_table();
1.44 parser 942: if(!table)
1.68 paf 943: throw Exception("parser.runtime",
1.91 paf 944: 0,
1.44 parser 945: "coordinates must be table");
1.13 paf 946:
1.92 paf 947: gdImage::Point* all_p=new(PointerFreeGC) gdImage::Point[table->count()];
1.44 parser 948: gdImage::Point *add_p=all_p;
949: table->for_each(add_point, &add_p);
1.108 paf 950: image.FilledPolygon(all_p, table->count(),
951: image.Color(params.as_int(0, "color must be int", r)));
1.12 paf 952: }
953:
1.16 paf 954: // font
955:
1.85 paf 956: #define Y(y)(y+index*height)
1.21 paf 957:
1.91 paf 958: // Font class
959:
960: const int Font::letter_spacing=1;
961:
962: Font::Font(//,
963: const String& aalphabet,
964: gdImage* aifont, int aheight, int amonospace, int aspacebarspace):
965: height(aheight), monospace(amonospace), spacebarspace(aspacebarspace),
1.100 paf 966: ifont(aifont),
967: alphabet(aalphabet) {
1.91 paf 968: }
969:
970: /* ******************************** char ********************************** */
971:
972: size_t Font::index_of(char ch) {
973: if(ch==' ') return STRING_NOT_FOUND;
974: return alphabet.pos(ch);
975: }
976:
977: int Font::index_width(size_t index) {
978: if(index==STRING_NOT_FOUND)
979: return spacebarspace;
980: int tr=ifont->GetTransparent();
981: for(int x=ifont->SX()-1; x>=0; x--) {
982: for(int y=0; y<height; y++)
983: if(ifont->GetPixel(x, Y(y))!=tr)
984: return x+1;
1.16 paf 985: }
1.91 paf 986: return 0;
987: }
988:
1.108 paf 989: void Font::index_display(gdImage& image, int x, int y, size_t index){
1.91 paf 990: if(index!=STRING_NOT_FOUND)
1.108 paf 991: ifont->Copy(image, x, y, 0, Y(0), index_width(index), height);
1.91 paf 992: }
993:
994: /* ******************************** string ********************************** */
995:
996: int Font::step_width(int index) {
997: return letter_spacing + (monospace ? monospace : index_width(index));
998: }
999:
1000: // counts trailing letter_spacing, consider this OK. useful for contiuations
1001: int Font::string_width(const String& s){
1002: const char* cstr=s.cstr();
1003: int result=0;
1004: for(const char* current=cstr; *current; current++)
1005: result+=step_width(index_of(*current));
1006: return result;
1007: }
1008:
1.108 paf 1009: void Font::string_display(gdImage& image, int x, int y, const String& s){
1.91 paf 1010: const char* cstr=s.cstr();
1011: for(const char* current=cstr; *current; current++) {
1012: size_t index=index_of(*current);
1013: index_display(image, x, y, index);
1014: x+=step_width(index);
1.87 paf 1015: }
1.91 paf 1016: }
1.87 paf 1017:
1.91 paf 1018: //
1.35 parser 1019:
1.16 paf 1020:
1.91 paf 1021: static void _font(Request& r, MethodParams& params) {
1022: const String& alphabet=params.as_string(0, "alphabet must not be code");
1023: gdImage* image=load(r, params.as_string(1, "file_name must not be code"));
1024: int spacebar_width=params.as_int(2, "spacebar_width must be int", r);
1.37 parser 1025: int monospace_width;
1.91 paf 1026: if(params.count()>3) {
1027: monospace_width=params.as_int(3, "monospace_width must be int", r);
1.37 parser 1028: if(!monospace_width)
1.91 paf 1029: monospace_width=image->SX();
1.37 parser 1030: } else
1031: monospace_width=0;
1.16 paf 1032:
1.91 paf 1033: if(!alphabet.length())
1.68 paf 1034: throw Exception("parser.runtime",
1.91 paf 1035: 0,
1.37 parser 1036: "alphabet must not be empty");
1.84 paf 1037:
1.91 paf 1038: if(int remainder=image->SY() % alphabet.length())
1.84 paf 1039: throw Exception("parser.runtime",
1.91 paf 1040: 0,
1.84 paf 1041: "font-file height(%d) not divisable by alphabet size(%d), remainder=%d",
1.91 paf 1042: image->SY(), alphabet.length(), remainder);
1.37 parser 1043:
1.108 paf 1044: GET_SELF(r, VImage).set_font(new Font(
1.37 parser 1045: alphabet,
1.36 parser 1046: image,
1.108 paf 1047: image->SY() / alphabet.length(), monospace_width, spacebar_width));
1.16 paf 1048: }
1049:
1.91 paf 1050: static void _text(Request& r, MethodParams& params) {
1051: int x=params.as_int(0, "x must be int", r);
1052: int y=params.as_int(1, "y must be int", r);
1053: const String& s=params.as_string(2, "text must not be code");
1.16 paf 1054:
1.91 paf 1055: VImage& vimage=GET_SELF(r, VImage);
1.108 paf 1056: vimage.font().string_display(vimage.image(), x, y, s);
1.16 paf 1057: }
1058:
1.91 paf 1059: static void _length(Request& r, MethodParams& params) {
1060: const String& s=params.as_string(0, "text must not be code");
1.47 parser 1061:
1.91 paf 1062: VImage& vimage=GET_SELF(r, VImage);
1.108 paf 1063: r.write_no_lang(*new VInt(vimage.font().string_width(s)));
1.47 parser 1064: }
1065:
1.91 paf 1066: static void _arc(Request& r, MethodParams& params) {
1.108 paf 1067: gdImage& image=GET_SELF(r, VImage).image();
1.48 parser 1068:
1.108 paf 1069: image.Arc(
1.91 paf 1070: params.as_int(0, "center_x must be int", r),
1071: params.as_int(1, "center_y must be int", r),
1072: params.as_int(2, "width must be int", r),
1073: params.as_int(3, "height must be int", r),
1074: params.as_int(4, "start degrees must be int", r),
1075: params.as_int(5, "end degrees must be int", r),
1.108 paf 1076: image.Color(params.as_int(6, "cx must be int", r)));
1.48 parser 1077: }
1078:
1.91 paf 1079: static void _sector(Request& r, MethodParams& params) {
1.108 paf 1080: gdImage& image=GET_SELF(r, VImage).image();
1.49 parser 1081:
1.108 paf 1082: image.Sector(
1.91 paf 1083: params.as_int(0, "center_x must be int", r),
1084: params.as_int(1, "center_y must be int", r),
1085: params.as_int(2, "width must be int", r),
1086: params.as_int(3, "height must be int", r),
1087: params.as_int(4, "start degrees must be int", r),
1088: params.as_int(5, "end degrees must be int", r),
1.108 paf 1089: image.Color(params.as_int(6, "color must be int", r)));
1.49 parser 1090: }
1091:
1.91 paf 1092: static void _circle(Request& r, MethodParams& params) {
1.108 paf 1093: gdImage& image=GET_SELF(r, VImage).image();
1.48 parser 1094:
1.91 paf 1095: int size=params.as_int(2, "radius must be int", r)*2;
1.108 paf 1096: image.Arc(
1.91 paf 1097: params.as_int(0, "center_x must be int", r),
1098: params.as_int(1, "center_y must be int", r),
1.50 parser 1099: size, //w
1100: size, //h
1.48 parser 1101: 0, //s
1102: 360, //e
1.108 paf 1103: image.Color(params.as_int(3, "color must be int", r)));
1.48 parser 1104: }
1105:
1.108 paf 1106: gdImage& as_image(MethodParams& params, int index, const char* msg) {
1.91 paf 1107: Value& value=params.as_no_junction(index, msg);
1.53 parser 1108:
1.91 paf 1109: if(Value* vimage=value.as(VIMAGE_TYPE, false)) {
1.108 paf 1110: return static_cast<VImage *>(vimage)->image();
1.75 paf 1111: } else
1.68 paf 1112: throw Exception("parser.runtime",
1.91 paf 1113: 0,
1.53 parser 1114: msg);
1115: }
1116:
1.91 paf 1117: static void _copy(Request& r, MethodParams& params) {
1.108 paf 1118: gdImage& dest=GET_SELF(r, VImage).image();
1.53 parser 1119:
1.108 paf 1120: gdImage& src=as_image(params, 0, "src must be image");
1.53 parser 1121:
1.91 paf 1122: int sx=params.as_int(1, "src_x must be int", r);
1123: int sy=params.as_int(2, "src_y must be int", r);
1124: int sw=params.as_int(3, "src_w must be int", r);
1125: int sh=params.as_int(4, "src_h must be int", r);
1126: int dx=params.as_int(5, "dest_x must be int", r);
1127: int dy=params.as_int(6, "dest_y must be int", r);
1128: if(params.count()>1+2+2+2) {
1129: int dw=params.as_int(1+2+2+2, "dest_w must be int", r);
1130: int dh=(int)(params.count()>1+2+2+2+1?
1131: params.as_int(1+2+2+2+1, "dest_h must be int", r):sh*(((double)dw)/((double)sw)));
1132: int tolerance=params.count()>1+2+2+2+2?
1133: params.as_int(1+2+2+2+2, "tolerance must be int", r):150;
1.53 parser 1134:
1.108 paf 1135: src.CopyResampled(dest, dx, dy, sx, sy, dw, dh, sw, sh, tolerance);
1.53 parser 1136: } else
1.108 paf 1137: src.Copy(dest, dx, dy, sx, sy, sw, sh);
1138: }
1139:
1140: static void _pixel(Request& r, MethodParams& params) {
1141: gdImage& image=GET_SELF(r, VImage).image();
1142:
1.109 paf 1143: int x=params.as_int(0, "x must be int", r);
1144: int y=params.as_int(1, "y must be int", r);
1145:
1146: if(params.count()>2) {
1147: image.SetPixel(x, y,
1148: image.Color(params.as_int(2, "color must be int", r)));
1149: } else
1150: r.write_no_lang(*new VInt(image.DecodeColor(image.GetPixel(x, y))));
1.53 parser 1151: }
1152:
1153:
1.22 paf 1154: // constructor
1155:
1.91 paf 1156: MImage::MImage(): Methoded("image") {
1.1 paf 1157: // ^image:measure[DATA]
1.22 paf 1158: add_native_method("measure", Method::CT_DYNAMIC, _measure, 1, 1);
1.3 paf 1159:
1.25 paf 1160: // ^image.html[]
1161: // ^image.html[hash]
1.22 paf 1162: add_native_method("html", Method::CT_DYNAMIC, _html, 0, 1);
1.6 paf 1163:
1.25 paf 1164: // ^image.load[background.gif]
1.22 paf 1165: add_native_method("load", Method::CT_DYNAMIC, _load, 1, 1);
1.6 paf 1166:
1.25 paf 1167: // ^image.create[width;height] bgcolor=white
1168: // ^image.create[width;height;bgcolor]
1.22 paf 1169: add_native_method("create", Method::CT_DYNAMIC, _create, 2, 3);
1.6 paf 1170:
1.25 paf 1171: // ^image.gif[]
1.95 paf 1172: add_native_method("gif", Method::CT_DYNAMIC, _gif, 0, 1);
1.12 paf 1173:
1.25 paf 1174: // ^image.line(x0;y0;x1;y1;color)
1.22 paf 1175: add_native_method("line", Method::CT_DYNAMIC, _line, 5, 5);
1.13 paf 1176:
1.25 paf 1177: // ^image.fill(x;y;color)
1.22 paf 1178: add_native_method("fill", Method::CT_DYNAMIC, _fill, 3, 3);
1.13 paf 1179:
1.25 paf 1180: // ^image.rectangle(x0;y0;x1;y1;color)
1.22 paf 1181: add_native_method("rectangle", Method::CT_DYNAMIC, _rectangle, 5, 5);
1.13 paf 1182:
1.25 paf 1183: // ^image.bar(x0;y0;x1;y1;color)
1.22 paf 1184: add_native_method("bar", Method::CT_DYNAMIC, _bar, 5, 5);
1.13 paf 1185:
1.44 parser 1186: // ^image.replace(color-source;color-dest)[table x:y]
1187: add_native_method("replace", Method::CT_DYNAMIC, _replace, 3, 3);
1188:
1189: // ^image.polyline(color)[table x:y]
1190: add_native_method("polyline", Method::CT_DYNAMIC, _polyline, 2, 2);
1.13 paf 1191:
1.44 parser 1192: // ^image.polygon(color)[table x:y]
1193: add_native_method("polygon", Method::CT_DYNAMIC, _polygon, 2, 2);
1.13 paf 1194:
1.44 parser 1195: // ^image.polybar(color)[table x:y]
1196: add_native_method("polybar", Method::CT_DYNAMIC, _polybar, 2, 2);
1.13 paf 1197:
1.36 parser 1198: // ^image.font[alPHAbet;font-file-name.gif](spacebar_width)
1199: // ^image.font[alPHAbet;font-file-name.gif](spacebar_width;width)
1200: add_native_method("font", Method::CT_DYNAMIC, _font, 3, 4);
1.16 paf 1201:
1.25 paf 1202: // ^image.text(x;y)[text]
1.22 paf 1203: add_native_method("text", Method::CT_DYNAMIC, _text, 3, 3);
1.47 parser 1204:
1205: // ^image.ngth[text]
1206: add_native_method("length", Method::CT_DYNAMIC, _length, 1, 1);
1.16 paf 1207:
1.48 parser 1208: // ^image.arc(center x;center y;width;height;start in degrees;end in degrees;color)
1209: add_native_method("arc", Method::CT_DYNAMIC, _arc, 7, 7);
1.49 parser 1210:
1211: // ^image.sector(center x;center y;width;height;start in degrees;end in degrees;color)
1212: add_native_method("sector", Method::CT_DYNAMIC, _sector, 7, 7);
1.48 parser 1213:
1214: // ^image.circle(center x;center y;r;color)
1215: add_native_method("circle", Method::CT_DYNAMIC, _circle, 4, 4);
1216:
1.56 parser 1217: // ^image.copy[source](src x;src y;src w;src h;dst x;dst y[;dest w[;dest h[;tolerance]]])
1218: add_native_method("copy", Method::CT_DYNAMIC, _copy, 1+2+2+2, (1+2+2+2)+2+1);
1.108 paf 1219:
1220: // ^image.pixel(x;y)[(color)]
1.109 paf 1221: add_native_method("pixel", Method::CT_DYNAMIC, _pixel, 2, 3);
1.1 paf 1222: }
E-mail: