OSDN Git Service

透過PNGのJPEG変換を回避する機能をTwitterPhotoクラスに移動
[opentween/open-tween.git] / OpenTween / Connection / TwitterPhoto.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3 //           (c) 2008-2011 Moz (@syo68k)
4 //           (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
5 //           (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6 //           (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7 //           (c) 2011      spinor (@tplantd) <http://d.hatena.ne.jp/spinor/>
8 //           (c) 2014      kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
9 // All rights reserved.
10 //
11 // This file is part of OpenTween.
12 //
13 // This program is free software; you can redistribute it and/or modify it
14 // under the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 3 of the License, or (at your option)
16 // any later version.
17 //
18 // This program is distributed in the hope that it will be useful, but
19 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 // for more details.
22 //
23 // You should have received a copy of the GNU General Public License along
24 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
25 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
26 // Boston, MA 02110-1301, USA.
27
28 using System;
29 using System.Collections.Generic;
30 using System.Drawing;
31 using System.Drawing.Imaging;
32 using System.IO;
33 using System.Linq;
34 using System.Threading.Tasks;
35 using OpenTween.Api.DataModel;
36 using OpenTween.Setting;
37
38 namespace OpenTween.Connection
39 {
40     public class TwitterPhoto : IMediaUploadService
41     {
42         private readonly string[] pictureExt = { ".jpg", ".jpeg", ".gif", ".png" };
43
44         private readonly Twitter tw;
45         private TwitterConfiguration twitterConfig;
46
47         public TwitterPhoto(Twitter twitter, TwitterConfiguration twitterConfig)
48         {
49             this.tw = twitter;
50             this.twitterConfig = twitterConfig;
51         }
52
53         public int MaxMediaCount => 4;
54
55         public string SupportedFormatsStrForDialog => "Image Files(*.gif;*.jpg;*.jpeg;*.png)|*.gif;*.jpg;*.jpeg;*.png";
56
57         public bool CanUseAltText => true;
58
59         public bool CheckFileExtension(string fileExtension)
60             => this.pictureExt.Contains(fileExtension, StringComparer.InvariantCultureIgnoreCase);
61
62         public bool CheckFileSize(string fileExtension, long fileSize)
63         {
64             var maxFileSize = this.GetMaxFileSize(fileExtension);
65             return maxFileSize == null || fileSize <= maxFileSize.Value;
66         }
67
68         public long? GetMaxFileSize(string fileExtension)
69             => this.twitterConfig.PhotoSizeLimit;
70
71         public async Task<PostStatusParams> UploadAsync(IMediaItem[] mediaItems, PostStatusParams postParams)
72         {
73             if (mediaItems == null)
74                 throw new ArgumentNullException(nameof(mediaItems));
75
76             if (mediaItems.Length == 0)
77                 throw new ArgumentException("Err:Media not specified.");
78
79             foreach (var item in mediaItems)
80             {
81                 if (item == null)
82                     throw new ArgumentException("Err:Media not specified.");
83
84                 if (!item.Exists)
85                     throw new ArgumentException("Err:Media not found.");
86             }
87
88             var uploadTasks = from m in mediaItems
89                               select this.UploadMediaItem(m);
90
91             var mediaIds = await Task.WhenAll(uploadTasks)
92                 .ConfigureAwait(false);
93
94             postParams.MediaIds = mediaIds;
95
96             return postParams;
97         }
98
99         // pic.twitter.com の URL は文字数にカウントされない
100         public int GetReservedTextLength(int mediaCount)
101             => 0;
102
103         public void UpdateTwitterConfiguration(TwitterConfiguration config)
104             => this.twitterConfig = config;
105
106         private async Task<long> UploadMediaItem(IMediaItem mediaItem)
107         {
108             async Task<long> UploadInternal(IMediaItem media)
109             {
110                 var mediaId = await this.tw.UploadMedia(media)
111                     .ConfigureAwait(false);
112
113                 if (!string.IsNullOrEmpty(media.AltText))
114                 {
115                     await this.tw.Api.MediaMetadataCreate(mediaId, media.AltText)
116                         .ConfigureAwait(false);
117                 }
118
119                 return mediaId;
120             }
121
122             using (var origImage = mediaItem.CreateImage())
123             {
124                 if (SettingManager.Common.AlphaPNGWorkaround && this.AddAlphaChannelIfNeeded(origImage.Image, out var newImage))
125                 {
126                     using (var newMediaItem = new MemoryImageMediaItem(newImage))
127                     {
128                         newMediaItem.AltText = mediaItem.AltText;
129                         return await UploadInternal(newMediaItem);
130                     }
131                 }
132                 else
133                 {
134                     return await UploadInternal(mediaItem);
135                 }
136             }
137         }
138
139         /// <summary>
140         /// pic.twitter.com アップロード時に JPEG への変換を回避するための加工を行う
141         /// </summary>
142         /// <remarks>
143         /// pic.twitter.com へのアップロード時に、アルファチャンネルを持たない PNG 画像が
144         /// JPEG 形式に変換され画質が低下する問題を回避します。
145         /// PNG 以外の画像や、すでにアルファチャンネルを持つ PNG 画像に対しては何もしません。
146         /// </remarks>
147         /// <returns>加工が行われた場合は true、そうでない場合は false</returns>
148         private bool AddAlphaChannelIfNeeded(Image origImage, out MemoryImage newImage)
149         {
150             newImage = null;
151
152             // PNG 画像以外に対しては何もしない
153             if (origImage.RawFormat.Guid != ImageFormat.Png.Guid)
154                 return false;
155
156             using (var bitmap = new Bitmap(origImage))
157             {
158                 // アルファ値が 255 以外のピクセルが含まれていた場合は何もしない
159                 foreach (var x in Enumerable.Range(0, bitmap.Width))
160                 {
161                     foreach (var y in Enumerable.Range(0, bitmap.Height))
162                     {
163                         if (bitmap.GetPixel(x, y).A != 255)
164                             return false;
165                     }
166                 }
167
168                 // 左上の 1px だけアルファ値を 254 にする
169                 var pixel = bitmap.GetPixel(0, 0);
170                 var newPixel = Color.FromArgb(pixel.A - 1, pixel.R, pixel.G, pixel.B);
171                 bitmap.SetPixel(0, 0, newPixel);
172
173                 // MemoryImage 作成時に画像はコピーされるため、この後 bitmap は破棄しても問題ない
174                 newImage = MemoryImage.CopyFromImage(bitmap);
175
176                 return true;
177             }
178         }
179     }
180 }