torch/qtlua

Bug with the qimage:save(f,[format])

nejyeah opened this issue · 7 comments

I want to draw some symbols in a picture. My code is as follows:

filename = "original_auto.jpg"
 qimage = qt.QImage.fromTensor(mrc:float():clone())  -- mrc is a torchTensor of mxn
 painter_image = qtwidget.newimage(qimage)
-- then pain something
painter_image:write(filename)

sometimes it runs ok,but sometimes an error report:
qlua: ...bioserver1/torch/install/share/lua/5.1/qtwidget/init.lua:127: image format 'jpg�' not supported for writing

then I go to see the file "qlua: ...bioserver1/torch/install/share/lua/5.1/qtwidget/init.lua", the function of write() as follows:

function qt.QtLuaPainter:write(...)
        local i = self:image()
        print(...)
        i:save(...)
end
```lua

I print the parameters '...' before "i:save(...)" , it is ok,just the right filename "original_auto.jpg".
"i:save(..)", 'i' refers to the Object of 'qt.QImage', the function guide is as follows. 

qimage:save(f,[format])
Expression qimage:save(f,format) saves the image data into file f. Argument f can be a file name or a Lua file descriptor. The file format is determined by the optional string format or by the file name extension. 

So the problem accurs exactly from the function "qimage:save(f,[format])". But i can not see the original code of the function, because these functions in "QtGUI" seems to be loaded from a dynamic lib '*.so'.  

This error message happens line 1275 in file https://github.com/torch/qtlua/blob/master/packages/qtgui/qtgui.cpp.

luaL_error(L, "image format '%s' not supported for writing", format);

Argument 'format' comes from the previous lines:

QString fn = luaQ_optqvariant(L, 2);
QByteArray fname = fn.toLocal8Bit();
format = strrchr(fname.constData(), '.');
format = luaL_optstring(L, 3, (format) ? format+1 : 0);

I cannot see how variable format could fail to be zero-terminated.

Since I am unable to replicate the bug here, could you insert a fprintf(stderr,"filename=[[%s]]\n", fname.constData()) before the error in order to see whether there are crazy characters there.
Regards,

L.
From: feng wang notifications@github.com
Reply-To: torch/qtlua reply@reply.github.com
Date: Monday, November 2, 2015 at 3:47 AM
To: torch/qtlua qtlua@noreply.github.com
Subject: [qtlua] Bug with the qimage:save(f,[format]) (#18)

qlua: ...bioserver1/torch/install/share/lua/5.1/qtwidget/init.lua:127: image format 'jpg�' not supported for writing

I add some 'fprintf' in the source code as follows:

1245 static int
1246 qimage_save(lua_State *L)
1247 {
1248   fprintf(stderr,"debug in the qimage_save");
1249   QImage s = luaQ_checkqvariant<QImage>(L, 1);
1250   QString fn = luaQ_optqvariant<QString>(L, 2);
1251   const char *format = 0;
1252   QFile f;
1253   if (fn.isEmpty() && lua_isuserdata(L, 2))
1254     {
1255       void *udata = luaL_checkudata(L, 2, LUA_FILEHANDLE);
1256       if (! f.open(*(FILE**)udata, QIODevice::WriteOnly))
1257         luaL_error(L,"cannot use stream for writing (%s)",
1258                    f.errorString().toLocal8Bit().constData() );
1259       format = luaL_checkstring(L, 3);
1260     }
1261   else
1262     {
1263       f.setFileName(fn);
1264       QByteArray fname = fn.toLocal8Bit();
1265       if (! f.open(QIODevice::WriteOnly))
1266         luaL_error(L,"cannot open '%s'for writing (%s)", fname.constData(),
1267                    f.errorString().toLocal8Bit().constData() );
1268       fprintf(stderr,"filename=[[%s]]\n",fname.constData());     --filename=[[/home/..../..*.jpg]]    
1269       format = strrchr(fname.constData(), '.');
1270       fprintf(stderr,"idex=[[%s]]\n",format);     --idex=[[.jpg]]
1271       format = luaL_optstring(L, 3, (format) ? format+1 : 0);
1272       fprintf(stderr,"format=[[%s]]\n",format);  --format=[[jpg]]
1273     }
1274   QImageWriter writer(&f, format);
1275   if (! writer.write(s))
1276     {
1277       f.remove();
1278       fprintf(stderr,"format=[[%s]]\n",format);  --format=[[+]]
1279       if (writer.error() == QImageWriter::UnsupportedFormatError)
1280         luaL_error(L, "image format '%s' not supported for writing", format);
1281       else
1282         luaL_error(L, "error while writing file (%s)",
1283                    f.errorString().toLocal8Bit().constData() );
1284     }
1285   return 0;
1286 }

SO i change the source code and reinstall torch. When error occurs:
"qlua: ...bioserver1/torch/install/share/lua/5.1/qtwidget/init.lua:126: image format '+' not supported for writing"
The print messages are showed in the above code. So the error occurs in

1274   QImageWriter writer(&f, format);
1275   if (! writer.write(s))

writer.write(s) must do something and change the content of format from 'jpg' to '+', So where is the source code of QT in torch?

@nejyeah are you in the EN locale? or is the native language of your system another language? This strongly looks like a Locale bug.

Oh,It still not help.
I change the locale in~/.vimrc,add:
export LC_ALL="en_US.UTF-8"
>$ source ~/.bashrc and reopen the terminal to make the change work.
>$ locale

LANG=en_US.UTF-8
LANGUAGE=en_US
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=en_US.UTF-8

The problem still occurs sometimes not must.
Sometimes it just run OK.
Sometimes it throws the error,the print information and errors messages as follows:

idex=[[.jpg]]
format=[[jpg]]
format=[[jpg�]]
qlua: ...bioserver1/torch/install/share/lua/5.1/qtwidget/init.lua:126: image format 'jpg�' not supported for writing

Sometimes it throws another error,the print information and errors messages as follows:

idex=[[.jpg]]
format=[[jpg]]
format=[[+]]
qlua: ...bioserver1/torch/install/share/lua/5.1/qtwidget/init.lua:126: image format '+' not supported for writing

What's the matter? By the way, each time I run my code with 'qlua', a message throws out:
"(qlua:19245): dconf-CRITICAL **: unable to create file '/run/user/1000/dconf/user': Permission denied. dconf will not work properly."
I am not sure whether it has something to do with the bug.

I believe my latest commit
b409256
fixes the problem.
Can you confirm?
Thanks.

p.s. -- I have no idea what's the dconf message. Must be related to your version of qt.

Yes, thanks, it seems to have solved my problems. I have run my code for hours successfully.

Good. Thanks for the bug report!