[MSPAINT] Support converting to black and white (#5554)

- Add ImageModel::IsBlackAndWhite and ImageModel::PushBlackAndWhite
  helper functions.
- Add CAttributesDialog::m_bBlackAndWhite.
- If IDD_ATTRIBUTESRB4 is checked, then make
  the bitmap black and white.
- Add IDS_LOSECOLOR to show message.
CORE-19094
This commit is contained in:
Katayama Hirofumi MZ 2023-08-11 20:27:12 +09:00 committed by GitHub
parent 97f59fa545
commit e6c23361a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
36 changed files with 100 additions and 2 deletions

View file

@ -103,10 +103,14 @@ LRESULT CAttributesDialog::OnInitDialog(UINT nMsg, WPARAM wParam, LPARAM lParam,
newHeight = imageModel.GetHeight();
CheckDlgButton(IDD_ATTRIBUTESRB3, BST_CHECKED);
CheckDlgButton(IDD_ATTRIBUTESRB5, BST_CHECKED);
SetDlgItemInt(IDD_ATTRIBUTESEDIT1, newWidth, FALSE);
SetDlgItemInt(IDD_ATTRIBUTESEDIT2, newHeight, FALSE);
if (imageModel.IsBlackAndWhite())
CheckRadioButton(IDD_ATTRIBUTESRB4, IDD_ATTRIBUTESRB5, IDD_ATTRIBUTESRB4);
else
CheckRadioButton(IDD_ATTRIBUTESRB4, IDD_ATTRIBUTESRB5, IDD_ATTRIBUTESRB5);
if (g_isAFile)
{
TCHAR date[100];
@ -142,6 +146,7 @@ LRESULT CAttributesDialog::OnClose(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL
LRESULT CAttributesDialog::OnOk(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
m_bBlackAndWhite = (IsDlgButtonChecked(IDD_ATTRIBUTESRB4) == BST_CHECKED);
EndDialog(1);
return 0;
}

View file

@ -64,6 +64,7 @@ public:
public:
int newWidth;
int newHeight;
BOOL m_bBlackAndWhite;
};
class CStretchSkewDialog : public CDialogImpl<CStretchSkewDialog>

View file

@ -267,3 +267,47 @@ HBITMAP ImageModel::CopyBitmap()
m_hbmOld = ::SelectObject(m_hDrawingDC, m_hBms[m_currInd]); // Re-select
return ret;
}
BOOL ImageModel::IsBlackAndWhite()
{
LONG cxWidth = GetWidth(), cyHeight = GetHeight();
for (LONG y = 0; y < cyHeight; ++y)
{
for (LONG x = 0; x < cxWidth; ++x)
{
COLORREF rgbColor = ::GetPixel(m_hDrawingDC, x, y);
if (rgbColor != RGB(0, 0, 0) && rgbColor != RGB(255, 255, 255))
return FALSE;
}
}
return TRUE;
}
void ImageModel::PushBlackAndWhite()
{
HBITMAP hNewBitmap = CopyBitmap();
if (!hNewBitmap)
return;
HDC hdc2 = ::CreateCompatibleDC(NULL);
HGDIOBJ hbm2Old = ::SelectObject(hdc2, hNewBitmap);
LONG cxWidth = GetWidth(), cyHeight = GetHeight();
for (LONG y = 0; y < cyHeight; ++y)
{
for (LONG x = 0; x < cxWidth; ++x)
{
COLORREF rgbColor = ::GetPixel(m_hDrawingDC, x, y);
BYTE Red = GetRValue(rgbColor);
BYTE Green = GetGValue(rgbColor);
BYTE Blue = GetBValue(rgbColor);
if ((Red + Green + Blue) / 3 >= 255 / 2)
::SetPixelV(hdc2, x, y, RGB(255, 255, 255)); // White
else
::SetPixelV(hdc2, x, y, RGB(0, 0, 0)); // Black
}
}
::SelectObject(hdc2, hbm2Old);
::DeleteDC(hdc2);
PushImageForUndo(hNewBitmap);
}

View file

@ -37,6 +37,8 @@ public:
void RotateNTimes90Degrees(int iN);
void Clamp(POINT& pt) const;
void NotifyImageChanged();
BOOL IsBlackAndWhite();
void PushBlackAndWhite();
protected:
HDC m_hDrawingDC; // The device context for this class

View file

@ -264,4 +264,5 @@ BEGIN
IDS_CANTPASTE "Failed to paste from the clipboard. The data format is either incorrect or not supported."
IDS_SAVEERROR "Failed to save the bitmap to file:\n\n%s"
IDS_CANTSENDMAIL "Failed to send a mail."
IDS_LOSECOLOR "The color information will be lost in this operation. Are you sure to proceed?"
END

View file

@ -264,4 +264,5 @@ BEGIN
IDS_CANTPASTE "Failed to paste from the clipboard. The data format is either incorrect or not supported."
IDS_SAVEERROR "Failed to save the bitmap to file:\n\n%s"
IDS_CANTSENDMAIL "Failed to send a mail."
IDS_LOSECOLOR "The color information will be lost in this operation. Are you sure to proceed?"
END

View file

@ -263,4 +263,5 @@ BEGIN
IDS_CANTPASTE "Failed to paste from the clipboard. The data format is either incorrect or not supported."
IDS_SAVEERROR "Failed to save the bitmap to file:\n\n%s"
IDS_CANTSENDMAIL "Failed to send a mail."
IDS_LOSECOLOR "The color information will be lost in this operation. Are you sure to proceed?"
END

View file

@ -263,4 +263,5 @@ BEGIN
IDS_CANTPASTE "Failed to paste from the clipboard. The data format is either incorrect or not supported."
IDS_SAVEERROR "Failed to save the bitmap to file:\n\n%s"
IDS_CANTSENDMAIL "Failed to send a mail."
IDS_LOSECOLOR "The color information will be lost in this operation. Are you sure to proceed?"
END

View file

@ -264,4 +264,5 @@ BEGIN
IDS_CANTPASTE "Failed to paste from the clipboard. The data format is either incorrect or not supported."
IDS_SAVEERROR "Failed to save the bitmap to file:\n\n%s"
IDS_CANTSENDMAIL "Failed to send a mail."
IDS_LOSECOLOR "The color information will be lost in this operation. Are you sure to proceed?"
END

View file

@ -266,4 +266,5 @@ BEGIN
IDS_CANTPASTE "Failed to paste from the clipboard. The data format is either incorrect or not supported."
IDS_SAVEERROR "Failed to save the bitmap to file:\n\n%s"
IDS_CANTSENDMAIL "Failed to send a mail."
IDS_LOSECOLOR "The color information will be lost in this operation. Are you sure to proceed?"
END

View file

@ -263,4 +263,5 @@ BEGIN
IDS_CANTPASTE "Failed to paste from the clipboard. The data format is either incorrect or not supported."
IDS_SAVEERROR "Failed to save the bitmap to file:\n\n%s"
IDS_CANTSENDMAIL "Failed to send a mail."
IDS_LOSECOLOR "The color information will be lost in this operation. Are you sure to proceed?"
END

View file

@ -264,4 +264,5 @@ BEGIN
IDS_CANTPASTE "Failed to paste from the clipboard. The data format is either incorrect or not supported."
IDS_SAVEERROR "Failed to save the bitmap to file:\n\n%s"
IDS_CANTSENDMAIL "Failed to send a mail."
IDS_LOSECOLOR "The color information will be lost in this operation. Are you sure to proceed?"
END

View file

@ -264,4 +264,5 @@ BEGIN
IDS_CANTPASTE "Failed to paste from the clipboard. The data format is either incorrect or not supported."
IDS_SAVEERROR "Failed to save the bitmap to file:\n\n%s"
IDS_CANTSENDMAIL "Failed to send a mail."
IDS_LOSECOLOR "The color information will be lost in this operation. Are you sure to proceed?"
END

View file

@ -266,4 +266,5 @@ BEGIN
IDS_CANTPASTE "Failed to paste from the clipboard. The data format is either incorrect or not supported."
IDS_SAVEERROR "Failed to save the bitmap to file:\n\n%s"
IDS_CANTSENDMAIL "Failed to send a mail."
IDS_LOSECOLOR "The color information will be lost in this operation. Are you sure to proceed?"
END

View file

@ -264,4 +264,5 @@ BEGIN
IDS_CANTPASTE "Failed to paste from the clipboard. The data format is either incorrect or not supported."
IDS_SAVEERROR "Failed to save the bitmap to file:\n\n%s"
IDS_CANTSENDMAIL "Failed to send a mail."
IDS_LOSECOLOR "The color information will be lost in this operation. Are you sure to proceed?"
END

View file

@ -263,4 +263,5 @@ BEGIN
IDS_CANTPASTE "Failed to paste from the clipboard. The data format is either incorrect or not supported."
IDS_SAVEERROR "Failed to save the bitmap to file:\n\n%s"
IDS_CANTSENDMAIL "Failed to send a mail."
IDS_LOSECOLOR "The color information will be lost in this operation. Are you sure to proceed?"
END

View file

@ -264,4 +264,5 @@ BEGIN
IDS_CANTPASTE "Failed to paste from the clipboard. The data format is either incorrect or not supported."
IDS_SAVEERROR "Failed to save the bitmap to file:\n\n%s"
IDS_CANTSENDMAIL "Failed to send a mail."
IDS_LOSECOLOR "The color information will be lost in this operation. Are you sure to proceed?"
END

View file

@ -265,4 +265,5 @@ BEGIN
IDS_CANTPASTE "クリップボードからの貼り付けに失敗しました。データ形式が間違っているか、未対応です。"
IDS_SAVEERROR "次のファイルとして画像を保存するのに失敗しました:\n\n%s"
IDS_CANTSENDMAIL "Failed to send a mail."
IDS_LOSECOLOR "The color information will be lost in this operation. Are you sure to proceed?"
END

View file

@ -263,4 +263,5 @@ BEGIN
IDS_CANTPASTE "Failed to paste from the clipboard. The data format is either incorrect or not supported."
IDS_SAVEERROR "Failed to save the bitmap to file:\n\n%s"
IDS_CANTSENDMAIL "Failed to send a mail."
IDS_LOSECOLOR "The color information will be lost in this operation. Are you sure to proceed?"
END

View file

@ -263,4 +263,5 @@ BEGIN
IDS_CANTPASTE "Failed to paste from the clipboard. The data format is either incorrect or not supported."
IDS_SAVEERROR "Failed to save the bitmap to file:\n\n%s"
IDS_CANTSENDMAIL "Failed to send a mail."
IDS_LOSECOLOR "The color information will be lost in this operation. Are you sure to proceed?"
END

View file

@ -266,4 +266,5 @@ BEGIN
IDS_CANTPASTE "Nie można wkleić ze schowka. Format danych jest nieprawidłowy lub nieobsługiwany."
IDS_SAVEERROR "Failed to save the bitmap to file:\n\n%s"
IDS_CANTSENDMAIL "Failed to send a mail."
IDS_LOSECOLOR "The color information will be lost in this operation. Are you sure to proceed?"
END

View file

@ -264,4 +264,5 @@ BEGIN
IDS_CANTPASTE "Failed to paste from the clipboard. The data format is either incorrect or not supported."
IDS_SAVEERROR "Failed to save the bitmap to file:\n\n%s"
IDS_CANTSENDMAIL "Failed to send a mail."
IDS_LOSECOLOR "The color information will be lost in this operation. Are you sure to proceed?"
END

View file

@ -264,4 +264,5 @@ BEGIN
IDS_CANTPASTE "Failed to paste from the clipboard. The data format is either incorrect or not supported."
IDS_SAVEERROR "Failed to save the bitmap to file:\n\n%s"
IDS_CANTSENDMAIL "Failed to send a mail."
IDS_LOSECOLOR "The color information will be lost in this operation. Are you sure to proceed?"
END

View file

@ -265,4 +265,5 @@ BEGIN
IDS_CANTPASTE "Nu a putut fi lipit din clipboard. Formatul de date este fie incorect, fie nesuportat."
IDS_SAVEERROR "Failed to save the bitmap to file:\n\n%s"
IDS_CANTSENDMAIL "Failed to send a mail."
IDS_LOSECOLOR "The color information will be lost in this operation. Are you sure to proceed?"
END

View file

@ -267,4 +267,5 @@ BEGIN
IDS_CANTPASTE "Не удалось вставить из буфера обмена. Формат данных либо некорректный, либо не поддерживается."
IDS_SAVEERROR "Не удалось сохранить точечный рисунок в файл:\n\n%s"
IDS_CANTSENDMAIL "Failed to send a mail."
IDS_LOSECOLOR "The color information will be lost in this operation. Are you sure to proceed?"
END

View file

@ -263,4 +263,5 @@ BEGIN
IDS_CANTPASTE "Failed to paste from the clipboard. The data format is either incorrect or not supported."
IDS_SAVEERROR "Failed to save the bitmap to file:\n\n%s"
IDS_CANTSENDMAIL "Failed to send a mail."
IDS_LOSECOLOR "The color information will be lost in this operation. Are you sure to proceed?"
END

View file

@ -263,4 +263,5 @@ BEGIN
IDS_CANTPASTE "Failed to paste from the clipboard. The data format is either incorrect or not supported."
IDS_SAVEERROR "Failed to save the bitmap to file:\n\n%s"
IDS_CANTSENDMAIL "Failed to send a mail."
IDS_LOSECOLOR "The color information will be lost in this operation. Are you sure to proceed?"
END

View file

@ -264,4 +264,5 @@ BEGIN
IDS_CANTPASTE "Failed to paste from the clipboard. The data format is either incorrect or not supported."
IDS_SAVEERROR "Failed to save the bitmap to file:\n\n%s"
IDS_CANTSENDMAIL "Failed to send a mail."
IDS_LOSECOLOR "The color information will be lost in this operation. Are you sure to proceed?"
END

View file

@ -264,4 +264,5 @@ BEGIN
IDS_CANTPASTE "Failed to paste from the clipboard. The data format is either incorrect or not supported."
IDS_SAVEERROR "Failed to save the bitmap to file:\n\n%s"
IDS_CANTSENDMAIL "Failed to send a mail."
IDS_LOSECOLOR "The color information will be lost in this operation. Are you sure to proceed?"
END

View file

@ -265,4 +265,5 @@ BEGIN
IDS_CANTPASTE "Failed to paste from the clipboard. The data format is either incorrect or not supported."
IDS_SAVEERROR "Failed to save the bitmap to file:\n\n%s"
IDS_CANTSENDMAIL "Failed to send a mail."
IDS_LOSECOLOR "The color information will be lost in this operation. Are you sure to proceed?"
END

View file

@ -263,4 +263,5 @@ BEGIN
IDS_CANTPASTE "Failed to paste from the clipboard. The data format is either incorrect or not supported."
IDS_SAVEERROR "Failed to save the bitmap to file:\n\n%s"
IDS_CANTSENDMAIL "Failed to send a mail."
IDS_LOSECOLOR "The color information will be lost in this operation. Are you sure to proceed?"
END

View file

@ -266,4 +266,5 @@ BEGIN
IDS_CANTPASTE "Failed to paste from the clipboard. The data format is either incorrect or not supported."
IDS_SAVEERROR "Failed to save the bitmap to file:\n\n%s"
IDS_CANTSENDMAIL "Failed to send a mail."
IDS_LOSECOLOR "The color information will be lost in this operation. Are you sure to proceed?"
END

View file

@ -264,4 +264,5 @@ BEGIN
IDS_CANTPASTE "Failed to paste from the clipboard. The data format is either incorrect or not supported."
IDS_SAVEERROR "Failed to save the bitmap to file:\n\n%s"
IDS_CANTSENDMAIL "Failed to send a mail."
IDS_LOSECOLOR "The color information will be lost in this operation. Are you sure to proceed?"
END

View file

@ -264,4 +264,5 @@ BEGIN
IDS_CANTPASTE "Failed to paste from the clipboard. The data format is either incorrect or not supported."
IDS_SAVEERROR "Failed to save the bitmap to file:\n\n%s"
IDS_CANTSENDMAIL "Failed to send a mail."
IDS_LOSECOLOR "The color information will be lost in this operation. Are you sure to proceed?"
END

View file

@ -222,3 +222,4 @@
#define IDS_CANTPASTE 940
#define IDS_SAVEERROR 941
#define IDS_CANTSENDMAIL 942
#define IDS_LOSECOLOR 943

View file

@ -949,7 +949,22 @@ LRESULT CMainWindow::OnCommand(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bH
{
if (attributesDialog.DoModal(mainWindow.m_hWnd))
{
imageModel.Crop(attributesDialog.newWidth, attributesDialog.newHeight, 0, 0);
if (attributesDialog.m_bBlackAndWhite && !imageModel.IsBlackAndWhite())
{
CString strText(MAKEINTRESOURCE(IDS_LOSECOLOR));
CString strTitle(MAKEINTRESOURCE(IDS_PROGRAMNAME));
INT id = MessageBox(strText, strTitle, MB_ICONINFORMATION | MB_YESNOCANCEL);
if (id != IDYES)
break;
imageModel.PushBlackAndWhite();
}
if (imageModel.GetWidth() != attributesDialog.newWidth ||
imageModel.GetHeight() != attributesDialog.newHeight)
{
imageModel.Crop(attributesDialog.newWidth, attributesDialog.newHeight);
}
}
break;
}