New: Import/Export of Cineon and DPX image files. The first is Kodak's
[blender.git] / source / blender / imbuf / intern / writeimage.c
1 /**
2  *
3  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version. The Blender
9  * Foundation also sells licenses for use in proprietary software under
10  * the Blender License.  See http://www.blender.org/BL/ for information
11  * about this.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL/BL DUAL LICENSE BLOCK *****
30  * writeimage.c
31  *
32  * $Id$
33  */
34
35 #ifdef WIN32
36 #include <io.h>
37 #endif
38
39 #include "BKE_global.h"
40 #include "BLI_blenlib.h"
41
42 #include "imbuf.h"
43 #include "imbuf_patch.h"
44
45 #include "IMB_imbuf_types.h"
46 #include "IMB_imbuf.h"
47
48 #include "IMB_allocimbuf.h"
49
50 #include "IMB_dpxcineon.h"
51 #include "IMB_targa.h"
52 #include "IMB_jpeg.h"
53 #include "IMB_iris.h"
54 #include "IMB_ham.h"
55 #include "IMB_hamx.h"
56 #include "IMB_amiga.h"
57 #include "IMB_png.h"
58 #include "IMB_bmp.h"
59 #include "IMB_tiff.h"
60 #include "IMB_radiance_hdr.h"
61 #ifdef WITH_OPENEXR
62 #include "openexr/openexr_api.h"
63 #endif
64
65 #include "IMB_iff.h"
66 #include "IMB_bitplanes.h"
67 #include "IMB_divers.h"
68
69 /* added facility to copy with saving non-float rects */
70
71 short IMB_saveiff(struct ImBuf *ibuf, char *name, int flags)
72 {
73         short ok=TRUE,delpl=FALSE;
74         int file = -1;
75
76         if (ibuf==0) return (FALSE);
77         ibuf->flags = flags;
78
79         /* Put formats that take a filename here */
80         if (IS_jpg(ibuf)) {
81                 if(ibuf->rect==NULL && ibuf->rect_float)
82                         IMB_rect_from_float(ibuf);
83                 return imb_savejpeg(ibuf, name, flags);
84         }
85         if (IS_radhdr(ibuf)) {
86                 return imb_savehdr(ibuf, name, flags);
87         }
88         if (IS_png(ibuf)) {
89                 if(ibuf->rect==NULL && ibuf->rect_float)
90                         IMB_rect_from_float(ibuf);
91                 return imb_savepng(ibuf, name, flags);
92         }
93         if (IS_bmp(ibuf)) {
94                 if(ibuf->rect==NULL && ibuf->rect_float)
95                         IMB_rect_from_float(ibuf);
96                 return imb_savebmp(ibuf, name, flags);
97         }
98         if (IS_tga(ibuf)) {
99                 if(ibuf->rect==NULL && ibuf->rect_float)
100                         IMB_rect_from_float(ibuf);
101                 return imb_savetarga(ibuf, name, flags);
102         }
103         if (IS_iris(ibuf)) {
104                 if(ibuf->rect==NULL && ibuf->rect_float)
105                         IMB_rect_from_float(ibuf);
106                 return imb_saveiris(ibuf, name, flags);
107         }
108         if (G.have_libtiff && IS_tiff(ibuf)) {
109                 if(ibuf->rect==NULL && ibuf->rect_float)
110                         IMB_rect_from_float(ibuf);
111                 return imb_savetiff(ibuf, name, flags);
112         }
113 #ifdef WITH_OPENEXR
114         if (IS_openexr(ibuf)) {
115                 return imb_save_openexr(ibuf, name, flags);
116         }
117 #endif
118         if (IS_cineon(ibuf)) {
119                 return imb_savecineon(ibuf, name, flags);
120                 
121         }
122         if (IS_dpx(ibuf)) {
123                 return imb_save_dpx(ibuf, name, flags);
124         }
125         file = open(name, O_BINARY | O_RDWR | O_CREAT | O_TRUNC, 0666);
126         if (file < 0) return (FALSE);
127
128         if (flags & IB_rect){
129                 if (ibuf->cmap){
130                         imb_checkncols(ibuf);
131                 }
132         }
133
134         /* Put formats that take a filehandle here */
135         ok = imb_start_iff(ibuf,file);
136         if (IS_amiga(ibuf)){
137                 IMB_flipy(ibuf);
138                 if (flags & IB_rect){
139                         if ((flags & IB_cmap) == 0) {
140                                 if (IS_ham(ibuf)){
141                                         if (ok) ok = imb_converttoham(ibuf);
142                                 }else if (ibuf->cmap){
143                                         if (ok) ok = IMB_converttocmap(ibuf);
144                                 }
145                         }
146                         if (ok){
147                                 if (ibuf->planes==0){
148                                         delpl=TRUE;
149                                         ok=imb_addplanesImBuf(ibuf);
150                                 }
151                                 imb_longtobp(ibuf);
152                         }
153                 }
154
155                 if (flags & IB_vert){
156                         if (ok) ok = imb_encodebodyv(ibuf,file);
157                 }
158                 else{
159                         if (ok) ok = imb_encodebodyh(ibuf,file);
160                 }
161                 if (ok) ok = imb_update_iff(file,BODY);
162         }else if (IS_anim(ibuf)) {
163                 if (ok) ok = imb_enc_anim(ibuf, file);
164                 if (ok) ok = imb_update_iff(file, BODY);
165         }
166         close(file);
167
168         if (ok==FALSE) {
169                 fprintf(stderr,"Couldn't save picture.\n");
170         }       
171         if (delpl) imb_freeplanesImBuf(ibuf);
172
173         return (ok);
174 }
175