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