Component to preview files in a directory

Post

Posted
Rating:
#1 (In Topic #623)
Regular
01McAc is in the usergroup ‘Regular’
I know there is the main component gb.form which provides the FileView class. Unfortunately, fileview does not preview all files (e.g. photo raw files) but Dolphin and Nautilus do preview these files. Dolphin uses qt5 so in return I would expect to find another fileview component in gb.qt5*- but  there isn't. Does anybody know another component in Gambas to show and preview files?
Online now: No Back to the top

Post

Posted
Rating:
#2
Banned
 qt5 can read a few formats but to read more the code has to be added to your application. that's what the other programs have done.

If you want to preview more files like nautilus and others then you will have to write the code as they have.
Online now: No Back to the top

Post

Posted
Rating:
#3
Banned
Or think of it this way…

it probably safe to assume that where gambas only uses the FileView qt widget other applications like Nautilus that are essentially one big FileView application have written their own complete file viewer control and do not just rely on the generic qt built in one.

Hope that makes sense
 :roll:
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Regular
stevedee is in the usergroup ‘Regular’
Pretty much as I said on the 14th March, you need to find a utility (e.g. simple command-line program, or maybe a library) that you can utilise to do this task.

I suggest you take a look at Phil Harvey's Exiftool as this has an argument called JpgFromRaw and can be used in batch mode. So I'm suggesting you extract jpeg files from all RAWs in a directory, save them in /tmp directory (so as not to clog up your hard drive) then display each one as required.

But my preference would be to look at LibRaw, as this has a nice looking C API that you could declare in Gambas (C API | LibRaw).

Maybe its;

Code

int libraw_unpack(libraw_data_t*);

function would do what you need.

By using LibRaw you may end up with some useful code that others could use when working with RAWs in Gambas.

For exiftool there is a friendly forum: ExifTool Forum - Index
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Guru
cogier is in the usergroup ‘Guru’
cogier is in the usergroup ‘GambOS Contributor’
Steve pointed me to the ExifTool, but I could not get the -JpgFromRaw to work. I did get the -PreviewImage to work. Have a look at the attached code with the following provisos:-
 
1/. You need to have the exiftool installed.
2/. My camera is a Canon 6D Mark II and the raw format is CR2, so this is the file type I tested this on.
3/. The program has no error catching code.

Attachment
Online now: No Back to the top

Post

Posted
Rating:
#6
Regular
01McAc is in the usergroup ‘Regular’
Thanks guys for all your advice and code.

stevedee said

For exiftool there is a friendly forum: ExifTool Forum - Index
Indeed this have been a helpful knowledge base for me. Attached is the alpha version of my tool. I use (of course) exiftool. A marvelous little program but cumbersome with all the parameters.

cogier said

but I could not get the -JpgFromRaw to work
I did- but -JpgFromRaw generates big files with original image size. What I actual need is just a tiny thumbnail. The parameter -PreviewImage generates a ~500kb file and a size of 1.440x960 which is actually too big for my purpose. exiftool cannot generate resized images out of a raw file, unfortunately. A pipe to another tool like imagemagick would be possible but all in all it takes too much time for a simple preview. Since I use exiftool my tool works for all supported raw files incl. CR2. The main focus is to view the GPS location on a map. The program has no error catching code either.

stevedee said

Maybe its;

Code: Select all

int libraw_unpack(libraw_data_t*);

function would do what you need.
Interesting you are mentioned this. I stumbled upon the libraw library earlier and looked for an example how to use libraries in general and libraw specifically. I had no luck and didn't find something so I went back to exiftool.

stevedee said

Have a look at the attached code with the following provisos:
Ultimately we have the same approach how to handle raw file. So I guess it is the right one;)

Attachment
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Regular
stevedee is in the usergroup ‘Regular’

01McAc said

… I stumbled upon the libraw library earlier and looked for an example how to use libraries in general and libraw specifically…

Don't know if you are still interested, but as its a poor TV night, I thought I'd take a look.

Turns out that the libraw C-API plays nice with Gambas.

Code (gambas)

  1. 'Just some demo code using the C-API of library: libraw
  2. 'Only needs libraw, a Gambas Form and a PictureBox
  3. '
  4. 'steve davis 23-Mar-2021
  5. '================================================
  6.  
  7. Library "libraw:16"
  8.  
  9. Public Extern libraw_init(iflags As Integer) As Pointer   'initialisation
  10. Public Extern libraw_close(libraw_data_t As Pointer)
  11. Public Extern libraw_version() As String
  12. Public Extern libraw_open_file(libraw_data_t As Pointer, sFileName As String) As Integer
  13. Public Extern libraw_unpack_thumb(libraw_data_t As Pointer) As Integer
  14. Public Extern libraw_dcraw_thumb_writer(libraw_data_t As Pointer, sFileName As String) As Integer
  15.  
  16. Const RAW_FILE_NAME As String = "sample1.dng"   'a sample RAW image file
  17. Const THUMB_FILE_NAME As String = "s1Thumb.jpg"   'the small extracted jpeg version
  18.  
  19.  
  20. Public Sub Form_Open()
  21. Dim pPoint As Pointer
  22. Dim intReply As Integer
  23. Dim strThumbNail As String
  24.  
  25.   'delete last demo image
  26.   strThumbNail = Application.Path & "/" & THUMB_FILE_NAME
  27.   If Exist(strThumbNail) Then
  28.     Kill strThumbNail
  29.  
  30.   'the fun starts here!
  31.   pPoint = libraw_init(0)
  32.   Me.Text = "libraw: " & libraw_version()
  33.   intReply = libraw_open_file(pPoint, Application.Path & "/" & RAW_FILE_NAME)
  34.   If intReply < 1 Then
  35.     intReply = libraw_unpack_thumb(pPoint)
  36.     If intReply < 1 Then
  37.       intReply = libraw_dcraw_thumb_writer(pPoint, strThumbNail)
  38.       If intReply < 1 Then
  39.         pBox.Image = Image.Load(strThumbNail)
  40.       Endif
  41.     Endif
  42.   libraw_close(pPoint)
  43.  
  44.  

…or download the project which includes a sample RAW file (.DNG)
Attachment

Although this extracts a 'thumbnail', this image has a resolution of 1024 x 683 for a file size of about 100k.

EDIT

I just found that with an Olympus RAW (.ORF) the thumbnail is much larger with a higher resolution.
Also, you should add

Code (gambas)

  1. pBox.Stretch = True
to the code above (& project file) after the image is loaded to the PictureBox.
Online now: No Back to the top

Post

Posted
Rating:
#8
Regular
01McAc is in the usergroup ‘Regular’

stevedee said

Don't know if you are still interested
Yes, I am. The code is exactly what I was looking for. What I can do is just copy and paste the code but, to be honest, I don't understand how a (any) library must be declared. How did you figure that out?

Re the poor TV night: how about a good book or going out for photography (night shooting with available light)? Helping people in the forum here might be a better idea:)
Online now: No Back to the top

Post

Posted
Rating:
#9
Avatar
Regular
stevedee is in the usergroup ‘Regular’

01McAc said

stevedee said

Don't know if you are still interested
…What I can do is just copy and paste the code but…

….I don't understand how a (any) library must be declared. How did you figure that out?

Its better not to just copy any of my examples…its much better to understand how its working and then write your own code better than my simple examples!

Here is the process I followed to establish what needed to be declared;
Knowing that libraw supported a C API, I opened Synaptic package manager and searched for libraw (If you also have a Debian/Ubuntu based Linux distro, this will make sense).
Although I had a package called: libraw1394 this was clearly nothing to do with RAW files. The only other package listed (and it was already installed) was: libraw16 described as a RAW image decoder library.
Initially I declared in Gambas;

Code (gambas)

  1. Library "libraw"
  2. Public Extern libraw_init(iflags As Integer) As Pointer
…but when I tried to run libraw_init in my code I got an error, something like;

Code

Cannot find dynamic library libraw.so
…where file.so is usually the library you are looking for.

So then I tried declaring the library as libraw16, but got a similar message.
Third-time-lucky when I declared: libraw:16
This format is basically using ":16" to define a kind of version. I say "kind of" because the actual version is reported as: 0.18.8
…but never let the facts get in the way of celebrating a 'success'

I then checked and verified that I was talking to this library by using the most basic function, declared and used something like this;

Code (gambas)

  1. Public Extern libraw_version() As String
  2. strVersion = libraw_version()

I covered the declaration of functions from a C API in a recent post: Gambas One - Gambas ONE   but if you have more questions, just ask.

Just how you decide to implement this solution will (I hope) be your choice, as I can't see much point in providing highly polished code. After all, you want to write code, not simply copy-and-paste what others dish up. But I will give a little bit of general advice. As your programs get bigger, it makes sense to take a modular approach.

I've taken my own example and moved the code into a Module called LibRAW, as the only stuff in a Form class should be code that is directly related to the Form. Here are two simple functions in this module;

Code (gambas)

  1. Public Function initialisation() As Boolean
  2.   'returns True if initialisation is successful
  3.  
  4.   pDataStructure = libraw_init(0)
  5.   If Not IsNull(pDataStructure) Then
  6.     Return True
  7.  
  8.  
  9.  
  10.   Return libraw_version()
  11.  

This makes using the library code in Gambas, more Gambas-like;

Code (gambas)

  1. Public Sub Form_Open()
  2.  
  3.   If LibRAW.initialisation() Then
  4.     Me.Text = "LibRaw version: " & LibRAW.version()
  5.     LibRAW.CloseLibraw()

…its a bit like a Gambas wrapper for the C library libraw…although not a great example.

I hope some of this nonsense helps!

Further reading:-
Captain Bodgit: Gambas: using external C libraries
/howto/extern - Gambas Documentation



Re the poor TV night: how about a good book or going out for photography (night shooting with available light)? Helping people in the forum here might be a better idea:)

Thanks for the tips.
I'm reading quite a bit at the moment and having a love affair with my 3D Printer. Like most love affairs, its about 50% fantastic and about 50% rubbish.

Its currently a bit too cold for my fragile body to venture out after dark. But I hope to re-build my strength and regain control of my body's thermostat in the very near future!
Online now: No Back to the top

Post

Posted
Rating:
#10
Regular
01McAc is in the usergroup ‘Regular’
Lots of investigation, thank you Sherlock  :)  I'll go through it in the next days.
Online now: No Back to the top

Post

Posted
Rating:
#11
Regular
01McAc is in the usergroup ‘Regular’
OK, downloaded your example and works. One change I had to do though. I needed to change the libraw version since I run Fedora rawhide and it obviously uses a different version:

Code (gambas)

  1. Library "libraw:20"
After this little change your example works as expected.
But - and it seems there is always a 'but' - when I try to get a thumbnail from my DNG's it generates a zero byte jpg. At first I thought it is a timing problem and put a

Code (gambas)

after the line

Code (gambas)

  1. intReply = libraw_dcraw_thumb_writer(pPoint, strThumbNail)
It still generates an empty jpg file.
I have been thinking about it and came to the conclusion that my DNG's don't have any embedded jpg as I am shooting raw only. libraw tries to extract the jpg and exiftool generates thumbnail out of the DNG I guess.
Nevertheless, it was a good exercise for me to get into the world of external libraries.
Online now: No Back to the top

Post

Posted
Rating:
#12
Avatar
Regular
stevedee is in the usergroup ‘Regular’

01McAc said

…I have been thinking about it and came to the conclusion that my DNG's don't have any embedded jpg as I am shooting raw only. libraw tries to extract the jpg and exiftool generates thumbnail out of the DNG I guess…

When you shoot RAW only on a camera, it should produce a RAW file with a jpeg copy built into the file. One reason for this is that your camera cannot display the RAW file, so needs a simple jpeg copy to show on your camera's screen.

As far as I know, exiftool cannot convert RAWs into jpeg copies.

I would be interested in a copy of your DNG file. Can you send via Firefox Send?
Online now: No Back to the top

Post

Posted
Rating:
#13
Avatar
Regular
stevedee is in the usergroup ‘Regular’
OK, sorry!

Forget Firefox Send, it looks like the service was disabled last September  …well nobody told me!
Online now: No Back to the top

Post

Posted
Rating:
#14
Avatar
Regular
stevedee is in the usergroup ‘Regular’
Thanks for sending me the image Detlef.

I can extract the jpeg from within the RAW file;
Image

(Click to enlarge)


…and the jpeg image file details (which is a 2.3MB file) look like this;
Image

(Click to enlarge)

Online now: No Back to the top

Post

Posted
Rating:
#15
Regular
01McAc is in the usergroup ‘Regular’
hmm, there must be something wrong in my code. The problem sits mostly in front of the monitor :shock:  
Did you extract the thumbnail with libraw and in the app you provided before for download?
Online now: No Back to the top

Post

Posted
Rating:
#16
Avatar
Regular
stevedee is in the usergroup ‘Regular’

01McAc said

hmm, there must be something wrong in my code…

I suggest you paste the libraw section of your code.
Online now: No Back to the top

Post

Posted
Rating:
#17
Regular
01McAc is in the usergroup ‘Regular’

stevedee said

I suggest you paste the libraw section of your code.
There must be some magic in the code. I double checked my code, made no changes et voilá: it works. Great stuff. Thank you.
The only remaining question is still the non-preview of the DNG in the FileView component. Dolphin and Nautilus (see attachment) both are able to show a tiny preview of DNG files. Dolphin is a QT based program and I presume Gambas uses the same components of QT.
Image

(Click to enlarge)

Online now: No Back to the top

Post

Posted
Rating:
#18
Banned

01McAc said

stevedee said

I suggest you paste the libraw section of your code.
There must be some magic in the code. I double checked my code, made no changes et voilá: it works. Great stuff. Thank you.
The only remaining question is still the non-preview of the DNG in the FileView component. Dolphin and Nautilus (see attachment) both are able to show a tiny preview of DNG files. Dolphin is a QT based program and I presume Gambas uses the same components of QT.
Screenshot_20210402_130237.jpg

It's got nothing to do with QT
Nautilus/Dolphin are doing the work not QT

It's funny how you seem to think,, Nautilus does it and it uses qt so gambas should too as it uses qt.
After it has been explained  :lol:
Online now: No Back to the top

Post

Posted
Rating:
#19
Avatar
Regular
stevedee is in the usergroup ‘Regular’

01McAc said


There must be some magic in the code. I double checked my code, made no changes et voilá: it works. Great stuff. Thank you.

You are very welcome!
… Dolphin and Nautilus … both are able to show a tiny preview of DNG files…

I'm 95% sure that gnome-raw-thumbnailer is installed on your system, and that file managers such as Nautilus use this to extract jpegs from some RAW files (I think you may have to alter settings or do something to get it to work with other types of RAW files such as {say} .CR2).

The file manager "Nemo" on my light-weight Peppermint system did not display RAW thumbnails until a few minutes ago, when I installed gnome-raw-thumbnailer, and now it does.

So what we need now is for some cleaver guy to modify the FileView class to get it to work with gnome-raw-thumbnailer
Online now: No Back to the top

Post

Posted
Rating:
#20
Avatar
Regular
stevedee is in the usergroup ‘Regular’
Let me retract the bit about .CR2 files.

gnome-raw-thumbnailer uses /usr/share/thumbnailers/gnome-raw.thumbnailer which contains this:-

Code

[Thumbnailer Entry]
TryExec=gnome-raw-thumbnailer
Exec=gnome-raw-thumbnailer -s %s %u %o
MimeType=image/x-adobe-dng;image/x-canon-cr2;image/x-canon-crw;image/x-dcraw;image/x-fuji-raf;image/x-kodak-dcr;image/x-kodak-k25;image/x-kodak-kdc;image/x-minolta-mrw;image/x-nikon-nef;image/x-olympus-orf;image/x-panasonic-raw;image/x-pentax-pef;image/x-sigma-x3f;image/x-sony-arw;image/x-sony-sr2;image/x-sony-srf;

…which does include support for .CR2 and many more.
Online now: No Back to the top

Post

Posted
Rating:
#21
Regular
01McAc is in the usergroup ‘Regular’
Same thing here. On Fedora Rawhide it's the raw-thumbnailer:

Code

[Thumbnailer Entry]
TryExec=/usr/bin/raw-thumbnailer
Exec=/usr/bin/raw-thumbnailer -s %s %u %o
MimeType=image/x-adobe-dng;image/x-canon-cr2;image/x-canon-crw;image/x-nikon-nef;image/x-olympus-orf;image/x-pentax-pef;image/x-sony-arw;image/x-epson-erf;image/x-minolta-mrw;
raw.thumbnailer (END)

BruceSteers said

So what we need now is for some cleaver guy to modify the FileView class to get it to work with gnome-raw-thumbnailer
Beyond my skills, I am very sorry. Any volunteers  :?:
Online now: No Back to the top

Post

Posted
Rating:
#22
Banned

01McAc said

Same thing here. On Fedora Rawhide it's the raw-thumbnailer:

Code

[Thumbnailer Entry]
TryExec=/usr/bin/raw-thumbnailer
Exec=/usr/bin/raw-thumbnailer -s %s %u %o
MimeType=image/x-adobe-dng;image/x-canon-cr2;image/x-canon-crw;image/x-nikon-nef;image/x-olympus-orf;image/x-pentax-pef;image/x-sony-arw;image/x-epson-erf;image/x-minolta-mrw;
raw.thumbnailer (END)

BruceSteers said

So what we need now is for some cleaver guy to modify the FileView class to get it to work with gnome-raw-thumbnailer
Beyond my skills, I am very sorry. Any volunteers  :?:


Haha i didn't write that..

why do i get the feeling if we were all sat in a room you'd all be looking at me right now ;) :lol:  :roll:

I highly doubt it is beyond anyone's skills here you just need to know where to add it.
I haven't really been following what's been going on here as it was a bit beyond me wich is why mostly all i have been able to advise is about how qt is not doing image processing itself.

Looks like FileView.class is in gb.form
comp/src/gb.form/.src/File/FileView.class · master · Gambas / gambas · GitLab
and the preview magic happens in CTaskPreview.class
comp/src/gb.form/.src/File/CTaskPreview.class · master · Gambas / gambas · GitLab

I would suggest pointing this thread to the gambas M/L so Benoit can see it , he would know much better how to implement it if you guys explain what you have discovered about how the other apps are doing things.
i could almost guarantee that my code would need re-writing for him lol.

Or if Benoit will not add it you could probably copy the 2 above mentioned files into your app and rename them (and change CTaskPreview references to match new name) and add it yourself.

I could help anyone if having problems but am doing other things at present to take this on.
Online now: No Back to the top

Post

Posted
Rating:
#23
Banned

BruceSteers said


Or if Benoit will not add it you could probably copy the 2 above mentioned files into your app and rename them (and change CTaskPreview references to match new name) and add it yourself.

I could help anyone if having problems but am doing other things at present to take this on.

I had a quick look and have this starter for you…

It's a test app i have just imported FileView.class and CTaskPreview.class into and called them MyFileView.class and MyTaskPreview.class

I had to pull a couple of other functions in from gb.form and the DirCache.class that i renamed to MyDirCache.Class

then changed any references and made a working app with it's own internal (customisable) fileview class.

I have not added any of your previewing stuff to it just made a base that you folks may find useful and be able to adapt to suit your own needs..
I also didn't add any location controls it just opens in Home/Pictures/


A raw previewer seems specific.
I don't have one unless i install one, thus for this reason i am not sure Benoit will want to add it. (I could well be wrong)
my thumbnailer list is as follows on Mint20
<CODE lang="shell">

Code

$ ls /usr/share/thumbnailers/
ffmpegthumbnailer.thumbnailer       librsvg.thumbnailer           xplayer.thumbnailer
gdk-pixbuf-thumbnailer.thumbnailer  mate-font-viewer.thumbnailer  xreader.thumbnailer

So might be better to make your own version
Hope this helps…
Online now: No Back to the top
1 guest and 0 members have just viewed this.