svn merge -r 17502:18705 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[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 #ifdef WIN32
33 #include <io.h>
34 #endif
35
36 #include "BKE_global.h"
37 #include "BLI_blenlib.h"
38
39 #include "imbuf.h"
40 #include "imbuf_patch.h"
41
42 #include "IMB_imbuf_types.h"
43 #include "IMB_imbuf.h"
44
45 #include "IMB_allocimbuf.h"
46
47 #include "IMB_dpxcineon.h"
48 #include "IMB_targa.h"
49 #include "IMB_jpeg.h"
50 #include "IMB_iris.h"
51 #include "IMB_ham.h"
52 #include "IMB_hamx.h"
53 #include "IMB_amiga.h"
54 #include "IMB_png.h"
55 #include "IMB_bmp.h"
56 #include "IMB_tiff.h"
57 #include "IMB_radiance_hdr.h"
58 #ifdef WITH_OPENJPEG
59 #include "IMB_jp2.h"
60 #endif
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 #ifdef WITH_OPENJPEG
136         if (IS_jp2(ibuf)) {
137                 return imb_savejp2(ibuf, name, flags);
138         }
139 #endif  
140         file = open(name, O_BINARY | O_RDWR | O_CREAT | O_TRUNC, 0666);
141         if (file < 0) return (FALSE);
142
143         if (flags & IB_rect){
144                 if (ibuf->cmap){
145                         imb_checkncols(ibuf);
146                 }
147         }
148
149         /* Put formats that take a filehandle here */
150         ok = imb_start_iff(ibuf,file);
151         if (IS_amiga(ibuf)){
152                 IMB_flipy(ibuf);
153                 if (flags & IB_rect){
154                         if ((flags & IB_cmap) == 0) {
155                                 if (IS_ham(ibuf)){
156                                         if (ok) ok = imb_converttoham(ibuf);
157                                 }else if (ibuf->cmap){
158                                         if (ok) ok = IMB_converttocmap(ibuf);
159                                 }
160                         }
161                         if (ok){
162                                 if (ibuf->planes==0){
163                                         delpl=TRUE;
164                                         ok=imb_addplanesImBuf(ibuf);
165                                 }
166                                 imb_longtobp(ibuf);
167                         }
168                 }
169
170                 if (flags & IB_vert){
171                         if (ok) ok = imb_encodebodyv(ibuf,file);
172                 }
173                 else{
174                         if (ok) ok = imb_encodebodyh(ibuf,file);
175                 }
176                 if (ok) ok = imb_update_iff(file,BODY);
177         }else if (IS_anim(ibuf)) {
178                 if (ok) ok = imb_enc_anim(ibuf, file);
179                 if (ok) ok = imb_update_iff(file, BODY);
180         }
181         close(file);
182
183         if (ok==FALSE) {
184                 fprintf(stderr,"Couldn't save picture.\n");
185         }       
186         if (delpl) imb_freeplanesImBuf(ibuf);
187
188         return (ok);
189 }
190