svn merge -r 14236:14505 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_OPENEXR
59 #include "openexr/openexr_api.h"
60 #endif
61 #ifdef WITH_DDS
62 #include "dds/dds_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 /* not supported yet
119 #ifdef WITH_DDS
120         if (IS_dds(ibuf)) {
121                 return imb_save_dds(ibuf, name, flags);
122         }
123 #endif
124 */
125         if (IS_cineon(ibuf)) {
126                 return imb_savecineon(ibuf, name, flags);
127                 
128         }
129         if (IS_dpx(ibuf)) {
130                 return imb_save_dpx(ibuf, name, flags);
131         }
132         file = open(name, O_BINARY | O_RDWR | O_CREAT | O_TRUNC, 0666);
133         if (file < 0) return (FALSE);
134
135         if (flags & IB_rect){
136                 if (ibuf->cmap){
137                         imb_checkncols(ibuf);
138                 }
139         }
140
141         /* Put formats that take a filehandle here */
142         ok = imb_start_iff(ibuf,file);
143         if (IS_amiga(ibuf)){
144                 IMB_flipy(ibuf);
145                 if (flags & IB_rect){
146                         if ((flags & IB_cmap) == 0) {
147                                 if (IS_ham(ibuf)){
148                                         if (ok) ok = imb_converttoham(ibuf);
149                                 }else if (ibuf->cmap){
150                                         if (ok) ok = IMB_converttocmap(ibuf);
151                                 }
152                         }
153                         if (ok){
154                                 if (ibuf->planes==0){
155                                         delpl=TRUE;
156                                         ok=imb_addplanesImBuf(ibuf);
157                                 }
158                                 imb_longtobp(ibuf);
159                         }
160                 }
161
162                 if (flags & IB_vert){
163                         if (ok) ok = imb_encodebodyv(ibuf,file);
164                 }
165                 else{
166                         if (ok) ok = imb_encodebodyh(ibuf,file);
167                 }
168                 if (ok) ok = imb_update_iff(file,BODY);
169         }else if (IS_anim(ibuf)) {
170                 if (ok) ok = imb_enc_anim(ibuf, file);
171                 if (ok) ok = imb_update_iff(file, BODY);
172         }
173         close(file);
174
175         if (ok==FALSE) {
176                 fprintf(stderr,"Couldn't save picture.\n");
177         }       
178         if (delpl) imb_freeplanesImBuf(ibuf);
179
180         return (ok);
181 }
182