Commit 72426b61 authored by alex@thinkpad's avatar alex@thinkpad
Browse files

Lua: added dryos.shooting_card.get_image_path to fix edge cases

from api_test.lua when image number wrapped around at 9999->0001

(this replaced the hardcoded logic from api_test.lua for getting previous/future still image paths)

--HG--
branch : lua_fix
parent 832ae7fd3381
......@@ -21,6 +21,7 @@
#include <fileprefix.h>
#include <string.h>
#include <config.h>
#include <propvalues.h>
#include "lua_common.h"
......@@ -28,6 +29,7 @@ static int luaCB_card_index(lua_State * L);
static int luaCB_card_newindex(lua_State * L);
static int luaCB_directory_index(lua_State * L);
static int luaCB_directory_newindex(lua_State * L);
static int luaCB_card_image_path(lua_State * L);
const char * lua_dryos_directory_fields[] =
{
......@@ -190,6 +192,13 @@ static void setboolfield (lua_State *L, const char *key, int value) {
lua_setfield(L, -2, key);
}
static const luaL_Reg card_funcs[] =
{
{ "image_path", luaCB_card_image_path },
{ NULL, NULL }
};
static int luaCB_dryos_index(lua_State * L)
{
LUA_PARAM_STRING_OPTIONAL(key, 2, "");
......@@ -253,6 +262,7 @@ static int luaCB_dryos_index(lua_State * L)
lua_setfield(L, -2, "_card_ptr");
lua_pushfstring(L, "%s:/", card->drive_letter);
lua_setfield(L, -2, "path");
luaL_setfuncs(L, card_funcs, 0);
lua_newtable(L);
lua_pushcfunction(L, luaCB_card_index);
lua_setfield(L, -2, "__index");
......@@ -489,6 +499,74 @@ static int luaCB_directory_newindex(lua_State * L)
// Inherits from @{directory}
// @type card
/***
Returns current/previous/future still image path. Examples:
- `B:/DCIM/100CANON/IMG1234.CR2` (with extension)
- `B:/DCIM/100CANON/IMG1234` (without extension)
@tparam[opt=0] int file_offset 0 = last saved image, positive = future images, negative = previous images.
@tparam[opt=nil] ?string|nil = preferred extension, nil = automatic.
@function image_path
@treturn string
*/
static int luaCB_card_image_path(lua_State * L)
{
if(!lua_istable(L, 1)) return luaL_argerror(L, 1, "expected table");
if(lua_getfield(L, 1, "_card_ptr") == LUA_TLIGHTUSERDATA)
{
struct card_info * card = lua_touserdata(L, -1);
LUA_PARAM_INT_OPTIONAL(file_offset, 2, 0);
LUA_PARAM_STRING_OPTIONAL(extension, 3, 0);
char image_path[32]; /* B:/DCIM/100CANON/IMG_1234.CR2 */
int folder_num = card->folder_number;
int file_num = card->file_number;
for (int i = 0; i != file_offset; i += SGN(file_offset))
{
file_num += SGN(file_offset);
if (file_num > 9999)
{
file_num = 1;
folder_num++;
if (folder_num > 999)
{
folder_num = 100;
}
}
else if (file_num < 1)
{
file_num = 9999;
folder_num--;
if (folder_num < 100)
{
folder_num = 999;
}
}
}
int raw = pic_quality & 0x60000;
snprintf(image_path, sizeof(image_path),
"%s:/DCIM/%03d%s/%s%04d%s",
card->drive_letter, /* B */
folder_num, /* 100 */
get_dcim_dir_suffix(), /* CANON */
get_file_prefix(), /* IMG_ */
file_num, /* 1234 */
extension ? extension : raw ? ".CR2" : ".JPG"
);
lua_pushstring(L, image_path);
return 1;
}
else
{
return luaL_error(L, "could not get lightuserdata for card");
}
}
static int luaCB_card_index(lua_State * L)
{
if(!lua_istable(L, 1)) return luaL_argerror(L, 1, "expected table");
......
......@@ -970,7 +970,6 @@ end
function test_camera_take_pics()
printf("Testing picture taking functions...\n")
local initial_file_num
request_mode(MODE.M, "M")
camera.shutter = 1/50
......@@ -978,7 +977,7 @@ function test_camera_take_pics()
printf("Snap simulation test...\n")
assert(menu.set("Shoot Preferences", "Snap Simulation", 1))
initial_file_num = dryos.shooting_card.file_number
local initial_file_num = dryos.shooting_card.file_number
camera.shoot()
assert(dryos.shooting_card.file_number == initial_file_num)
assert(menu.set("Shoot Preferences", "Snap Simulation", 0))
......@@ -987,17 +986,31 @@ function test_camera_take_pics()
printf("Single picture...\n")
-- let's also check if we can find the image file
-- fixme: a way to check these routines when the image number wraps around at 10000
initial_file_num = dryos.shooting_card.file_number
local image_path = dryos.dcim_dir.path .. dryos.image_prefix .. string.format("%04d", initial_file_num % 9999 + 1)
local image_path_cr2 = image_path .. ".CR2"
local image_path_jpg = image_path .. ".JPG"
local image_path_cr2 = dryos.shooting_card:image_path(1, ".CR2") -- next image (assume CR2)
local image_path_jpg = dryos.shooting_card:image_path(1, ".JPG") -- next image (assume JPG)
local image_path_auto = dryos.shooting_card:image_path(1) -- next image (autodetect extension)
assert(dryos.shooting_card:image_path(1, nil) == image_path_auto)
assert(image_path_auto == image_path_cr2 or image_path_auto == image_path_jpg)
assert(dryos.shooting_card:image_path(1, "") .. ".CR2" == image_path_cr2)
assert(dryos.shooting_card:image_path(1, "") .. ".JPG" == image_path_jpg)
-- the image file(s) should not be present before taking the picture :)
assert(io.open(image_path_cr2, "rb") == nil)
assert(io.open(image_path_jpg, "rb") == nil)
camera.shoot()
assert(dryos.shooting_card:image_path(0) == image_path_auto) -- last captured image
assert(dryos.shooting_card:image_path() == image_path_auto) -- that's the default
-- manually build last image path name
-- next image path is harder to build manually, as you need to take care
-- of wrapping around (100CANON/IMG_9999.CR2 -> 101CANON/IMG_0001.CR2)
-- this is why dryos.shooting_card:image_path is preferred -- it handles these edge cases for you
local image_path_dcim = dryos.dcim_dir.path .. dryos.image_prefix .. string.format("%04d", dryos.shooting_card.file_number)
assert(image_path_dcim == dryos.shooting_card:image_path(0, ""))
-- but either CR2 or JPG should be there afterwards (or maybe both)
assert((dryos.shooting_card.file_number - initial_file_num) % 9999 == 1)
camera.wait()
......@@ -1022,12 +1035,12 @@ function test_camera_take_pics()
local old_prefix = dryos.image_prefix
dryos.image_prefix = "ABC_"
assert(dryos.image_prefix == "ABC_")
local image1_path = dryos.dcim_dir.path .. dryos.image_prefix .. string.format("%04d", initial_file_num % 9999 + 1)
local image1_path_cr2 = image1_path .. ".CR2"
local image1_path_jpg = image1_path .. ".JPG"
local image2_path = dryos.dcim_dir.path .. dryos.image_prefix .. string.format("%04d", (initial_file_num + 1) % 9999 + 1)
local image2_path_cr2 = image2_path .. ".CR2"
local image2_path_jpg = image2_path .. ".JPG"
-- edge case if image number is 9997 (next image 9998) before running api_test.lua
-- (image1 will be 100CANON/IMG_9999 and image2 should be 101CANON/IMG_0001)
local image1_path_cr2 = dryos.shooting_card:image_path(1, ".CR2")
local image1_path_jpg = dryos.shooting_card:image_path(1, ".JPG")
local image2_path_cr2 = dryos.shooting_card:image_path(2, ".CR2")
local image2_path_jpg = dryos.shooting_card:image_path(2, ".JPG")
assert(io.open(image1_path_cr2, "rb") == nil)
assert(io.open(image1_path_jpg, "rb") == nil)
assert(io.open(image2_path_cr2, "rb") == nil)
......@@ -1056,6 +1069,11 @@ function test_camera_take_pics()
dryos.image_prefix = ""
assert(dryos.image_prefix == old_prefix)
-- edge case if image number is 9994/9995 (next image 9995/9996) before running api_test.lua
-- images from previous tests will be either (9995, 9996, 9997), or (9996, 9997, 9998)
-- the next 3 images will be either (100CANON/IMG_9998, 100CANON/IMG_9999, 101CANON/IMG_0001)
-- or (100CANON/IMG_9999, 101CANON/IMG_0001, 101CANON/IMG_0002)
-- dryos.shooting_card:image_path takes care of this
printf("Bracketed pictures...\n")
initial_file_num = dryos.shooting_card.file_number
camera.shutter.value = 1/500
......@@ -1067,10 +1085,9 @@ function test_camera_take_pics()
assert((dryos.shooting_card.file_number - initial_file_num) % 9999 == 3)
camera.wait()
-- fixme: how to check metadata in the files?
for i = dryos.shooting_card.file_number - 2, dryos.shooting_card.file_number do
image_path = dryos.dcim_dir.path .. dryos.image_prefix .. string.format("%04d", (i - 1) % 9999 + 1)
image_path_cr2 = image_path .. ".CR2"
image_path_jpg = image_path .. ".JPG"
for i = -2,0 do
image_path_cr2 = dryos.shooting_card:image_path(i, ".CR2")
image_path_jpg = dryos.shooting_card:image_path(i, ".JPG")
local size_cr2 = print_file_size(image_path_cr2)
local size_jpg = print_file_size(image_path_jpg)
assert(size_cr2 or size_jpg)
......@@ -1081,9 +1098,8 @@ function test_camera_take_pics()
printf("Bulb picture...\n")
local t0 = dryos.ms_clock
initial_file_num = dryos.shooting_card.file_number
image_path = dryos.dcim_dir.path .. dryos.image_prefix .. string.format("%04d", initial_file_num % 9999 + 1)
image_path_cr2 = image_path .. ".CR2"
image_path_jpg = image_path .. ".JPG"
image_path_cr2 = dryos.shooting_card:image_path(1, ".CR2")
image_path_jpg = dryos.shooting_card:image_path(1, ".JPG")
assert(io.open(image_path_cr2, "rb") == nil)
assert(io.open(image_path_jpg, "rb") == nil)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment