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