1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "exif.h"
#include "jpeg.h"
struct impl {
SV *file_name;
struct exiftags *et;
};
typedef struct impl *Image__EXIF;
#ifndef Newxz
#define Newxz(ptr, n, type) Newz(705, ptr, n, type)
#endif
static void
load(pTHX_ struct impl *impl, const char *name)
{
int mark, first = 0;
unsigned int len, rlen;
unsigned char *exifbuf = NULL;
FILE *fp = fopen(name, "rb");
if (!fp)
croak("Can't open file %s: %s", name, strerror(errno));
while (jpegscan(fp, &mark, &len, !(first++))) {
if (mark != JPEG_M_APP1) {
if (fseek(fp, len, SEEK_CUR)) {
free(exifbuf);
fclose(fp);
croak("Can't seek in file %s: %s", name, strerror(errno));
}
continue;
}
exifbuf = (unsigned char *) malloc(len);
if (!exifbuf) {
fclose(fp);
croak("malloc failed");
}
rlen = fread(exifbuf, 1, len, fp);
if (rlen != len) {
free(exifbuf);
fclose(fp);
croak("error reading JPEG %s: length mismatch", name);
}
impl->et = exifparse(exifbuf, len);
break;
}
if (impl->et && !impl->et->props) {
exiffree(impl->et);
impl->et = 0;
}
free(exifbuf);
fclose(fp);
}
static STRLEN
trimmed_len(const char *p)
{
const char *endp = p + strlen(p);
while (endp > p) {
endp--;
if (!isspace(*endp))
return endp - p + 1;
}
return 0;
}
static SV *
get_props(pTHX_ struct impl *impl, unsigned short lvl)
{
struct exifprop *ep;
HV *hv = 0;
if (!impl->file_name)
croak("no Image::EXIF data loaded");
if (!impl->et)
return &PL_sv_undef;
for (ep = impl->et->props; ep; ep = ep->next) {
const char *name;
if (ep->lvl == ED_PAS)
/* Take care of point-and-shoot values. */
ep->lvl = ED_CAM;
else if (ep->lvl == ED_OVR || ep->lvl == ED_BAD)
/* For now, just treat overridden & bad values as verbose. */
ep->lvl = ED_VRB;
if (ep->lvl != lvl)
continue;
name = ep->descr ? ep->descr : ep->name;
if (!name || !*name)
continue;
if (!hv)
hv = newHV();
hv_store(hv, name, strlen(name),
ep->str ? newSVpvn(ep->str, trimmed_len(ep->str))
: newSViv(ep->value), 0);
}
return hv ? newRV_noinc((SV *) hv) : &PL_sv_undef;
}
MODULE = Image::EXIF PACKAGE = Image::EXIF
PROTOTYPES: DISABLE
Image::EXIF
_new_instance(package)
char *package
CODE:
struct impl *impl;
Newxz(impl, 1, struct impl);
RETVAL = impl;
OUTPUT:
RETVAL
void
_destroy_instance(impl)
Image::EXIF impl
CODE:
if (impl->file_name)
SvREFCNT_dec(impl->file_name);
if (impl->et)
exiffree(impl->et);
Safefree(impl);
void
_load_file(impl, file_name)
Image::EXIF impl
SV *file_name;
CODE:
load(aTHX_ impl, SvPV_nolen(file_name));
impl->file_name = SvREFCNT_inc(file_name);
SV *
_file_name(impl)
Image::EXIF impl
CODE:
RETVAL = newSVsv(impl->file_name);
OUTPUT:
RETVAL
SV *
get_camera_info(impl)
Image::EXIF impl
CODE:
RETVAL = get_props(aTHX_ impl, ED_CAM);
OUTPUT:
RETVAL
SV *
get_image_info(impl)
Image::EXIF impl
CODE:
RETVAL = get_props(aTHX_ impl, ED_IMG);
OUTPUT:
RETVAL
SV *
get_other_info(impl)
Image::EXIF impl
CODE:
RETVAL = get_props(aTHX_ impl, ED_VRB);
OUTPUT:
RETVAL
SV *
get_unknown_info(impl)
Image::EXIF impl
CODE:
RETVAL = get_props(aTHX_ impl, ED_UNK);
OUTPUT:
RETVAL
|