This commit is a modified version of patch #6860
[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 #ifdef WITH_DDS
65 #include "dds/dds_api.h"
66 #endif
67
68 #include "IMB_iff.h"
69 #include "IMB_bitplanes.h"
70 #include "IMB_divers.h"
71
72 /* added facility to copy with saving non-float rects */
73
74 short IMB_saveiff(struct ImBuf *ibuf, char *name, int flags)
75 {
76         short ok=TRUE,delpl=FALSE;
77         int file = -1;
78
79         if (ibuf==0) return (FALSE);
80         ibuf->flags = flags;
81
82         /* Put formats that take a filename here */
83         if (IS_jpg(ibuf)) {
84                 if(ibuf->rect==NULL && ibuf->rect_float)
85                         IMB_rect_from_float(ibuf);
86                 return imb_savejpeg(ibuf, name, flags);
87         }
88         if (IS_radhdr(ibuf)) {
89                 return imb_savehdr(ibuf, name, flags);
90         }
91         if (IS_png(ibuf)) {
92                 if(ibuf->rect==NULL && ibuf->rect_float)
93                         IMB_rect_from_float(ibuf);
94                 return imb_savepng(ibuf, name, flags);
95         }
96         if (IS_bmp(ibuf)) {
97                 if(ibuf->rect==NULL && ibuf->rect_float)
98                         IMB_rect_from_float(ibuf);
99                 return imb_savebmp(ibuf, name, flags);
100         }
101         if (IS_tga(ibuf)) {
102                 if(ibuf->rect==NULL && ibuf->rect_float)
103                         IMB_rect_from_float(ibuf);
104                 return imb_savetarga(ibuf, name, flags);
105         }
106         if (IS_iris(ibuf)) {
107                 if(ibuf->rect==NULL && ibuf->rect_float)
108                         IMB_rect_from_float(ibuf);
109                 return imb_saveiris(ibuf, name, flags);
110         }
111         if (G.have_libtiff && IS_tiff(ibuf)) {
112                 if(ibuf->rect==NULL && ibuf->rect_float)
113                         IMB_rect_from_float(ibuf);
114                 return imb_savetiff(ibuf, name, flags);
115         }
116 #ifdef WITH_OPENEXR
117         if (IS_openexr(ibuf)) {
118                 return imb_save_openexr(ibuf, name, flags);
119         }
120 #endif
121 /* not supported yet
122 #ifdef WITH_DDS
123         if (IS_dds(ibuf)) {
124                 return imb_save_dds(ibuf, name, flags);
125         }
126 #endif
127 */
128         if (IS_cineon(ibuf)) {
129                 return imb_savecineon(ibuf, name, flags);
130                 
131         }
132         if (IS_dpx(ibuf)) {
133                 return imb_save_dpx(ibuf, name, flags);
134         }
135         file = open(name, O_BINARY | O_RDWR | O_CREAT | O_TRUNC, 0666);
136         if (file < 0) return (FALSE);
137
138         if (flags & IB_rect){
139                 if (ibuf->cmap){
140                         imb_checkncols(ibuf);
141                 }
142         }
143
144         /* Put formats that take a filehandle here */
145         ok = imb_start_iff(ibuf,file);
146         if (IS_amiga(ibuf)){
147                 IMB_flipy(ibuf);
148                 if (flags & IB_rect){
149                         if ((flags & IB_cmap) == 0) {
150                                 if (IS_ham(ibuf)){
151                                         if (ok) ok = imb_converttoham(ibuf);
152                                 }else if (ibuf->cmap){
153                                         if (ok) ok = IMB_converttocmap(ibuf);
154                                 }
155                         }
156                         if (ok){
157                                 if (ibuf->planes==0){
158                                         delpl=TRUE;
159                                         ok=imb_addplanesImBuf(ibuf);
160                                 }
161                                 imb_longtobp(ibuf);
162                         }
163                 }
164
165                 if (flags & IB_vert){
166                         if (ok) ok = imb_encodebodyv(ibuf,file);
167                 }
168                 else{
169                         if (ok) ok = imb_encodebodyh(ibuf,file);
170                 }
171                 if (ok) ok = imb_update_iff(file,BODY);
172         }else if (IS_anim(ibuf)) {
173                 if (ok) ok = imb_enc_anim(ibuf, file);
174                 if (ok) ok = imb_update_iff(file, BODY);
175         }
176         close(file);
177
178         if (ok==FALSE) {
179                 fprintf(stderr,"Couldn't save picture.\n");
180         }       
181         if (delpl) imb_freeplanesImBuf(ibuf);
182
183         return (ok);
184 }
185