1
0

Merge pull request #65 from chadparry/rawphoto

Raw photo support
This commit is contained in:
Bharat Mediratta 2011-08-20 22:01:09 -07:00
commit 53abda7602
7 changed files with 409 additions and 0 deletions

View File

@ -0,0 +1,74 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2011 Chad Parry
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class Admin_RawPhoto_Controller extends Admin_Controller {
public function index() {
print $this->_get_view();
}
private function _get_view($errors = array(), $icc_path = null) {
$dcraw = rawphoto_graphics::detect_dcraw();
rawphoto_graphics::report_dcraw_support($dcraw);
$view = new Admin_View("admin.html");
$view->content = new View("admin_rawphoto.html");
$view->content->is_keeporiginal_active = module::is_active("keeporiginal");
$view->content->dcraw = $dcraw;
$toolkit_names = array("imagemagick" => "ImageMagick",
"graphicsmagick" => "GraphicsMagick");
$toolkit_id = module::get_var("gallery", "graphics_toolkit");
$toolkit_names = rawphoto_graphics::get_supported_toolkits();
$view->content->toolkit_name = array_key_exists($toolkit_id, $toolkit_names) ?
$toolkit_names[$toolkit_id] : "none";
$view->content->icc_path = isset($icc_path) ?
$icc_path : module::get_var("rawphoto", "icc_path");
$view->content->errors = $errors;
return $view;
}
public function saveprefs() {
// Prevent Cross Site Request Forgery
access::verify_csrf();
$post = new Validation($_POST);
$post->add_callbacks("IccPath", array($this, "_validate_icc_path"));
$icc_path = Input::instance()->post("IccPath");
if ($post->validate()) {
module::set_var("rawphoto", "icc_path", $icc_path);
message::success(t("Your preferences have been saved."));
} else {
message::error(t("Your preferences are not valid."));
}
print $this->_get_view($post->errors(), $icc_path);
}
public function _validate_icc_path(Validation $post, $field) {
if (!empty($post->$field)) {
if (!@is_file($post->$field)) {
$post->add_error($field, t("No ICC profile exists at the location <code>%icc_path</code>",
array("icc_path" => $post->$field)));
}
$dcraw = rawphoto_graphics::detect_dcraw();
if (version_compare($dcraw->version, "8.00", "<")) {
$post->add_error($field, t("Versions of <em>dcraw</em> before <code>8.00</code> do not support an ICC profile"));
}
}
}
}

View File

@ -0,0 +1,66 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2011 Chad Parry
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class rawphoto_event_Core {
static function item_created($item) {
if ($item->is_photo()) {
$input_file = $item->file_path();
$output_file = system::temp_filename("rawphoto-", "jpg");
$success = rawphoto_graphics::convert($input_file, $output_file);
if ($success) {
$item->set_data_file($output_file);
$item->save();
unlink($output_file);
}
}
}
static function admin_menu($menu, $theme) {
$menu->get("settings_menu")
->append(Menu::factory("link")
->id("rawphoto")
->label(t("Raw Photos"))
->url(url::site("admin/rawphoto")));
}
static function legal_photo_extensions($extensions_wrapper) {
array_push($extensions_wrapper->extensions,
"3fr", "arw", "bay", "bmq", "cr2", "crw", "cs1", "dc2", "dcr", "dng", "erf",
"fff", "k25", "kdc", "mef", "mos", "mrw", "nef", "orf", "pef", "raf", "raw",
"rdc", "rw2", "sr2", "srf", "x3f");
}
static function legal_photo_types($types_wrapper) {
array_push($types_wrapper->types,
// Most raw photos are detected as TIFF.
"image/tiff",
// Minolta raw photos are mis-detected as wireless bitmap format.
"image/vnd.wap.wbmp",
// All other raw photos have unrecognized formats.
"");
}
static function module_change($changes) {
rawphoto_version::report_item_conversion_support();
}
static function graphics_toolkit_change($toolkit_id) {
rawphoto_graphics::report_ppm_support($toolkit_id);
}
}

View File

@ -0,0 +1,128 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2011 Chad Parry
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class rawphoto_graphics {
static function detect_dcraw() {
$dcraw = new stdClass();
$path = system::find_binary("dcraw");
if (empty($path)) {
$dcraw->installed = false;
$dcraw->error = t("The <em>dcraw</em> tool could not be located on your system.");
} else {
$dcraw->path = $path;
if (!@is_file($path)) {
$dcraw->installed = false;
$dcraw->error = t("The <em>dcraw</em> tool is installed, " .
"but PHP's <code>open_basedir</code> restriction " .
"prevents Gallery from using it.");
} else if (!preg_match('/^Raw [Pp]hoto [Dd]ecoder(?: "dcraw")? v(\S+)$/m',
shell_exec(escapeshellcmd($path) . " 2>&1"), $matches)) {
$dcraw->installed = false;
$dcraw->error = t("The <em>dcraw</em> tool is installed, " .
"but the version is not recognized.");
} else {
$dcraw->version = $matches[1];
$dcraw->installed = true;
}
}
return $dcraw;
}
static function get_supported_toolkits() {
return array("imagemagick" => "ImageMagick",
"graphicsmagick" => "GraphicsMagick");
}
static function report_dcraw_support($dcraw) {
if ($dcraw->installed) {
site_status::clear("rawphoto_needs_dcraw");
} else {
site_status::warning(
t('The <em>Raw Photos</em> module requires the <a href="%dcraw_url"><em>dcraw</em> tool</a> to be installed.',
array("dcraw_url" => "http://www.cybercom.net/~dcoffin/dcraw/")),
"rawphoto_needs_dcraw");
}
}
static function report_ppm_support($toolkit_id) {
if (array_key_exists($toolkit_id, rawphoto_graphics::get_supported_toolkits())) {
site_status::clear("rawphoto_needs_ppm_support");
} else {
site_status::warning(
t('The <em>Raw Photos</em> module requires a supporting graphics toolkit. ' .
'<a href="%activate_url">Activate</a> either ImageMagick or GraphicsMagick.',
array("activate_url" => url::site("admin/graphics"))),
"rawphoto_needs_ppm_support");
}
}
static function convert($input_file, $output_file) {
$success = false;
$dcraw = rawphoto_graphics::detect_dcraw();
if ($dcraw->installed) {
// Use dcraw to convert from a raw image to a standard pixmap.
$cmd = escapeshellcmd($dcraw->path) . " -c -w ";
if (version_compare($dcraw->version, "6.00", ">=")) {
$cmd .= "-t 0 ";
}
if (version_compare($dcraw->version, "8.81", ">=")) {
$cmd .= "-W ";
}
$icc_path = module::get_var("rawphoto", "icc_path");
if (!empty($icc_path) && version_compare($dcraw->version, "8.00", ">=")) {
$cmd .= "-p " . escapeshellarg($icc_path) . " ";
}
$cmd .= escapeshellarg($input_file);
// Then use the graphics toolkit to convert the stream to a JPEG.
$cmd .= " | ";
$toolkit_id = module::get_var("gallery", "graphics_toolkit");
$toolkit_path = module::get_var("gallery", "graphics_toolkit_path");
$image_quality = module::get_var("gallery", "image_quality");
$toolkit_compat = false;
switch ($toolkit_id) {
case 'imagemagick':
$cmd .= escapeshellcmd("$toolkit_path/convert");
$cmd .= " -quality " . escapeshellarg($image_quality . "%");
$cmd .= " - " . escapeshellarg($output_file);
$toolkit_compat = true;
break;
case 'graphicsmagick':
$cmd .= escapeshellcmd("$toolkit_path/gm");
$cmd .= " convert -quality " . escapeshellarg($image_quality . "%");
$cmd .= " - " . escapeshellarg($output_file);
$toolkit_compat = true;
break;
default:
log::warning("rawphoto", "Cannot convert raw photo with graphics toolkit: " .
$toolkit_id->active);
}
if ($toolkit_compat) {
exec($cmd, $output, $return_var);
// Failure is common, because dcraw will abort unless the original image is a raw photo.
$success = ($return_var == 0);
if (!$success) {
@unlink($output_file);
}
}
}
return $success;
}
}

View File

@ -0,0 +1,38 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2011 Chad Parry
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class rawphoto_installer {
static function install() {
module::set_version("rawphoto", 3);
}
static function activate() {
rawphoto_version::report_item_conversion_support();
$dcraw = rawphoto_graphics::detect_dcraw();
rawphoto_graphics::report_dcraw_support($dcraw);
$toolkit_id = module::get_var("gallery", "graphics_toolkit");
rawphoto_graphics::report_ppm_support($toolkit_id);
}
static function deactivate() {
site_status::clear("rawphoto_needs_item_conversion_support");
site_status::clear("rawphoto_needs_dcraw");
site_status::clear("rawphoto_needs_ppm_support");
}
}

View File

@ -0,0 +1,46 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2011 Chad Parry
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class rawphoto_version {
const MIN_RELEASE_VERSION = "3.0.3";
const MIN_BUILD_NUMBER = "164";
static function report_item_conversion_support() {
if (gallery::RELEASE_CHANNEL == "release") {
if (version_compare(gallery::VERSION, rawphoto_version::MIN_RELEASE_VERSION, ">=")) {
site_status::clear("rawphoto_needs_item_conversion_support");
} else {
site_status::warning(
t("The <em>Raw Photos</em> module requires Gallery %version or higher.",
array("version" => rawphoto_version::MIN_RELEASE_VERSION)),
"rawphoto_needs_item_conversion_support");
}
} else {
if (version_compare(gallery::build_number(), rawphoto_version::MIN_BUILD_NUMBER, ">=")) {
site_status::clear("rawphoto_needs_item_conversion_support");
} else {
site_status::warning(
t("The <em>Raw Photos</em> module requires Gallery %version, build %build_number or higher.",
array("version" => gallery::VERSION,
"build_number" => rawphoto_version::MIN_BUILD_NUMBER)),
"rawphoto_needs_item_conversion_support");
}
}
}
}

View File

@ -0,0 +1,7 @@
name = "Raw Photos"
description = "Use raw photos produced by a digital camera."
version = 3
author_name = "Chad Parry"
author_url = "http://codex.gallery2.org/User:Chadparry"
info_url = "http://codex.gallery2.org/Gallery3:Modules:rawphoto"
discuss_url = "http://gallery.menalto.com/node/101817"

View File

@ -0,0 +1,50 @@
<?php defined("SYSPATH") or die("No direct script access.") ?>
<div id="g-rawphoto-admin">
<h2> <?= t("Raw Photos") ?> </h2>
<?= form::open("admin/rawphoto/saveprefs") ?>
<?= access::csrf_form_field() ?>
<p><?= t('<a href="%raw_url">Raw photo</a> processing depends on the ' .
'<a href="%dcraw_url"><em>dcraw</em> tool</a>, which must be installed separately. ' .
'It also depends on either the ImageMagick or GraphicsMagick graphics toolkits.',
array("raw_url" =>
"http://www.adamcoupe.com/whitepapers/photography_technique_benefits_of_shooting_in_raw.htm",
"dcraw_url" => "http://www.cybercom.net/~dcoffin/dcraw/")) ?></p>
<? if (!$is_keeporiginal_active): ?>
<p class="g-module-status g-warning g-block">
<?= t('The photos will not be preserved in their raw format unless you ' .
'<a href="%modules_url">activate the <em>Keep Original</em> module</a>.',
array("modules_url" => url::site("admin/modules"))) ?>
</p>
<? endif; ?>
<fieldset>
<legend><?= t("Paths") ?></legend>
<? if ($dcraw->installed): ?>
<p><?= t("The <em>dcraw</em> tool was detected at <code>%path</code> " .
"with version <code>%version</code>.",
array("path" => $dcraw->path, "version" => $dcraw->version)) ?></p>
<? else: ?>
<p class="g-module-status g-error g-block"><?= $dcraw->error ?></p>
<? endif; ?>
<? if ($toolkit_name == "none"): ?>
<p class="g-module-status g-error g-block">
<?= t('No suitable graphics toolkit was detected. ' .
'<a href="%activate_url">Activate</a> either ImageMagick or GraphicsMagick.',
array("activate_url" => url::site("admin/graphics"))) ?>
</p>
<? else: ?>
<p><?= t("The %toolkit_name graphics toolkit was detected.",
array("toolkit_name" => $toolkit_name)) ?></p>
<? endif; ?>
<? if (!empty($errors["IccPath"])): ?>
<p class="g-module-status g-error g-block"><?= $errors["IccPath"] ?></p>
<? endif; ?>
<?= form::label("IccPath", t('Path to <a href="%icc_url">ICC profile</a>', array("icc_url" =>
"http://www.permajet.com/30/Downloads/76/What_are_ICC_Profiles,_and_why_do_I_need_them.html"))) ?>
<?= form::input(array("name" => "IccPath", "id" => "IccPath"), $icc_path) ?>
<?if (empty($icc_path) || !empty($errors["IccPath"])): ?>
<em>An ICC profile is optional. If you don't know what it is, then you don't need it.</em>
<? endif; ?>
</fieldset>
<?= form::submit("SavePrefs", "Save") ?>
</form>
</div>