|
|
| version 1.69, 2002/04/10 09:53:14 | version 1.73, 2002/08/01 11:26:43 |
|---|---|
| Line 3 | Line 3 |
| Copyright(c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) | Copyright(c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| $Id$ | |
| */ | */ |
| static const char* IDENT_IMAGE_C="$Id$"; | |
| /* | /* |
| jpegsize: gets the width and height (in pixels) of a jpeg file | jpegsize: gets the width and height (in pixels) of a jpeg file |
| Andrew Tong, werdna@ugcs.caltech.edu February 14, 1995 | Andrew Tong, werdna@ugcs.caltech.edu February 14, 1995 |
| Line 25 | Line 25 |
| #include "pa_vfile.h" | #include "pa_vfile.h" |
| #include "pa_vimage.h" | #include "pa_vimage.h" |
| // defines | |
| #define IMAGE_CLASS_NAME "image" | |
| // class | // class |
| class MImage : public Methoded { | class MImage : public Methoded { |
| Line 83 private: | Line 79 private: |
| }; | }; |
| #endif | #endif |
| /// PNG file header | |
| struct PNG_Header { | |
| char dummy[12]; | |
| char signature[4]; //< must be "IHDR" | |
| unsigned char high_width[2]; //< image width high bytes [we ignore for now] | |
| unsigned char width[2]; //< image width low bytes | |
| unsigned char high_height[2]; //< image height high bytes [we ignore for now] | |
| unsigned char height[4]; //< image height | |
| }; | |
| /// GIF file header | /// GIF file header |
| struct GIF_Header { | struct GIF_Header { |
| char type[3]; // 'GIF' | char signature[3]; // 'GIF' |
| char version[3]; | char version[3]; |
| unsigned char width[2]; | unsigned char width[2]; |
| unsigned char height[2]; | unsigned char height[2]; |
| Line 133 void measure_gif(Pool& pool, const Strin | Line 139 void measure_gif(Pool& pool, const Strin |
| "not GIF file - too small"); | "not GIF file - too small"); |
| GIF_Header *head=(GIF_Header *)buf; | GIF_Header *head=(GIF_Header *)buf; |
| if(strncmp(head->type, "GIF", 3)!=0) | if(strncmp(head->signature, "GIF", 3)!=0) |
| throw Exception("image.format", | throw Exception("image.format", |
| origin_string, | origin_string, |
| "not GIF file - wrong signature"); | "not GIF file - wrong signature"); |
| Line 199 void measure_jpeg(Pool& pool, const Stri | Line 205 void measure_jpeg(Pool& pool, const Stri |
| "broken JPEG file - size frame not found"); | "broken JPEG file - size frame not found"); |
| } | } |
| void measure_png(Pool& pool, const String *origin_string, | |
| Measure_reader& reader, int& width, int& height) { | |
| void *buf; | |
| const int head_size=sizeof(PNG_Header); | |
| if(reader.read(buf, head_size)<head_size) | |
| throw Exception("image.format", | |
| origin_string, | |
| "not PNG file - too small"); | |
| PNG_Header *head=(PNG_Header *)buf; | |
| if(strncmp(head->signature, "IHDR", 4)!=0) | |
| throw Exception("image.format", | |
| origin_string, | |
| "not PNG file - wrong signature"); | |
| width=big_endian_to_int(head->width); | |
| height=big_endian_to_int(head->height); | |
| } | |
| // measure center | // measure center |
| void measure(Pool& pool, const String& file_name, | void measure(Pool& pool, const String& file_name, |
| Line 209 void measure(Pool& pool, const String& f | Line 235 void measure(Pool& pool, const String& f |
| measure_gif(pool, &file_name, reader, width, height); | measure_gif(pool, &file_name, reader, width, height); |
| else if(strcasecmp(cext, "JPG")==0 || strcasecmp(cext, "JPEG")==0) | else if(strcasecmp(cext, "JPG")==0 || strcasecmp(cext, "JPEG")==0) |
| measure_jpeg(pool, &file_name, reader, width, height); | measure_jpeg(pool, &file_name, reader, width, height); |
| else if(strcasecmp(cext, "PNG")==0) | |
| measure_png(pool, &file_name, reader, width, height); | |
| else | else |
| throw Exception("image.format", | throw Exception("image.format", |
| &file_name, | &file_name, |
| Line 316 static void _html(Request& r, const Stri | Line 344 static void _html(Request& r, const Stri |
| if(params->size()) { | if(params->size()) { |
| // for backward compatibility: someday was ^html{} | // for backward compatibility: someday was ^html{} |
| Value &vattribs=r.process_to_value(params->get(0), | Value &vattribs=r.process_to_value(params->get(0), |
| 0/*no name*/, | /*0/*no name* /,*/ |
| false/*don't intercept string*/); | false/*don't intercept string*/); |
| if(vattribs.is_defined()) // allow 'void' | if(vattribs.is_defined()) // allow 'void' |
| if(attribs=vattribs.get_hash(&method_name)) { | if(attribs=vattribs.get_hash(&method_name)) { |
| Line 693 static void _length(Request& r, const St | Line 721 static void _length(Request& r, const St |
| VImage& vimage=*static_cast<VImage *>(r.self); | VImage& vimage=*static_cast<VImage *>(r.self); |
| if(vimage.image) | if(vimage.image) |
| if(vimage.font) { | if(vimage.font) { |
| VInt& result=*new(pool) VInt(pool, vimage.font->string_width(s)); | r.write_no_lang(*new(pool) VInt(pool, vimage.font->string_width(s))); |
| result.set_name(method_name); | |
| r.write_assign_lang(result); | |
| } else | } else |
| throw Exception("parser.runtime", | throw Exception("parser.runtime", |
| &method_name, | &method_name, |
| Line 814 static void _copy(Request& r, const Stri | Line 840 static void _copy(Request& r, const Stri |
| // constructor | // constructor |
| MImage::MImage(Pool& apool) : Methoded(apool) { | MImage::MImage(Pool& apool) : Methoded(apool, "image") { |
| set_name(*NEW String(pool(), IMAGE_CLASS_NAME)); | |
| // ^image:measure[DATA] | // ^image:measure[DATA] |
| add_native_method("measure", Method::CT_DYNAMIC, _measure, 1, 1); | add_native_method("measure", Method::CT_DYNAMIC, _measure, 1, 1); |