xref: /haiku/src/add-ons/translators/raw/main.cpp (revision 90ae2e54f6ccaca73c011a2aa4cdd660417108ad)
1 /*
2  * Copyright 2007, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "RAWTranslator.h"
8 #include "RAW.h"
9 #include "TranslatorWindow.h"
10 
11 #include <Application.h>
12 #include <Catalog.h>
13 
14 #define TEST_MODE 0
15 #define SHOW_MODE 1
16 #if SHOW_MODE && TEST_MODE
17 #	include <Bitmap.h>
18 #	include <BitmapStream.h>
19 #	include <LayoutBuilder.h>
20 #	include <String.h>
21 #	include <View.h>
22 #	include <Window.h>
23 #endif
24 
25 #include <stdio.h>
26 #include <string.h>
27 
28 #undef B_TRANSLATION_CONTEXT
29 #define B_TRANSLATION_CONTEXT "RAWTranslator main"
30 
31 int
32 main(int argc, char **argv)
33 {
34 	BApplication app("application/x-vnd.Haiku-RAWTranslator");
35 
36 #if TEST_MODE
37 	if (argc > 1) {
38 		for (int i = 1; i < argc; i++) {
39 			BFile file;
40 			status_t status = file.SetTo(argv[i], B_READ_ONLY);
41 			if (status != B_OK) {
42 				fprintf(stderr, "Cannot read file %s: %s\n", argv[i],
43 					strerror(status));
44 				continue;
45 			}
46 
47 			DCRaw raw(file);
48 
49 			try {
50 				status = raw.Identify();
51 			} catch (status_t error) {
52 				status = error;
53 			}
54 
55 			if (status < B_OK) {
56 				fprintf(stderr, "Could not identify file %s: %s\n",
57 					argv[i], strerror(status));
58 				continue;
59 			}
60 
61 			image_meta_info meta;
62 			raw.GetMetaInfo(meta);
63 
64 			printf("manufacturer: %s\n", meta.manufacturer);
65 			printf("model: %s\n", meta.model);
66 			printf("software: %s\n", meta.software);
67 			printf("flash used: %g\n", meta.flash_used);
68 			printf("ISO speed: %g\n", meta.iso_speed);
69 			if (meta.shutter >= 1)
70 				printf("shutter: %g sec\n", meta.shutter);
71 			else
72 				printf("shutter: 1/%g sec\n", 1 / meta.shutter);
73 			printf("aperture: %g\n", meta.aperture);
74 			printf("focal length: %g mm\n", meta.focal_length);
75 			printf("pixel aspect: %g\n", meta.pixel_aspect);
76 			printf("flip: %d\n", meta.flip);
77 			printf("shot order: %ld\n", meta.shot_order);
78 			printf("DNG version: %ld\n", meta.dng_version);
79 			printf("Camera White Balance:");
80 			for (int32 i = 0; i < 4; i++) {
81 				printf(" %g", meta.camera_multipliers[i]);
82 			}
83 			putchar('\n');
84 
85 			for (int32 i = 0; i < (int32)raw.CountImages(); i++) {
86 				image_data_info data;
87 				raw.ImageAt(i, data);
88 
89 				printf("  [%ld] %s %lu x %lu (%ld bits per sample, compression %ld)\n",
90 					i, data.is_raw ? "RAW " : "JPEG",
91 					data.width, data.height, data.bits_per_sample, data.compression);
92 
93 #	if SHOW_MODE
94 				if (!data.is_raw) {
95 					// write data to file
96 					uint8* buffer;
97 					size_t bufferSize;
98 					try {
99 						status = raw.ReadImageAt(i, buffer, bufferSize);
100 					} catch (status_t error) {
101 						status = error;
102 					}
103 					if (status == B_OK) {
104 						BString name = "/tmp/output";
105 						name << i << ".jpg";
106 						BFile output(name.String(),
107 							B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY);
108 						output.Write(buffer, bufferSize);
109 					}
110 				} else {
111 					RAWTranslator translator;
112 					BBitmapStream output;
113 					BBitmap* bitmap;
114 
115 					status_t status = translator.DerivedTranslate(&file, NULL,
116 						NULL, B_TRANSLATOR_BITMAP, &output, 0);
117 					if (status == B_OK)
118 						status = output.DetachBitmap(&bitmap);
119 					if (status == B_OK) {
120 						BWindow* window = new BWindow(BRect(0, 0, 1, 1),
121 							B_TRANSLATE("RAW"), B_TITLED_WINDOW,
122 							B_ASYNCHRONOUS_CONTROLS | B_NOT_RESIZABLE |
123 							B_AUTO_UPDATE_SIZE_LIMITS);
124 						BView* view = new BView(window->Bounds(), NULL,
125 							B_WILL_DRAW, B_FOLLOW_NONE);
126 						BLayoutBuilder::Group<>(window, B_HORIZONTAL)
127 							.Add(view);
128 						window->Show();
129 						snooze(300000);
130 						window->Lock();
131 						view->DrawBitmap(bitmap, window->Bounds());
132 						view->Sync();
133 						window->Unlock();
134 						delete bitmap;
135 
136 						wait_for_thread(window->Thread(), &status);
137 					}
138 				}
139 #	endif
140 			}
141 		}
142 		return 0;
143 	}
144 #endif
145 
146 	status_t status = LaunchTranslatorWindow(new RAWTranslator,
147 		B_TRANSLATE("RAW Settings"));
148 	if (status != B_OK)
149 		return 1;
150 
151 	app.Run();
152 	return 0;
153 }
154 
155